Jump to content

Recommended Posts

Hi,

 

Is it possible to use a loop within the PHP Mail function?  More specifically I need to use a loop in the body section of the mail funtion that echos each row fetched from a myql table.  I'm currently able to have a variable parsed correctly in the body section (e.g. $body = "$variable") which when emailed turns the variable into it's value. 

Link to comment
https://forums.phpfreaks.com/topic/50506-loop-in-mail-function/
Share on other sites

Well, since the mail function is a built-in function within PHP, you can't edit how it works. And it doesn't sound like you want to put the mail function within the loop. It sounds like you want the loop to generate data that is used in your mail function. Does something like this help?

 

<?php
// run a query
$query = "SELECT * FROM table";
$result = mysql_query($query) OR DIE (mysql_error());
// loop through the results and concatenate data to a "body variable"
while($r = mysql_fetch_array($result)) {
$body .= $r["some_field"];
}
// send the mail
mail($to, $subject, $body, $headers);
?>

Link to comment
https://forums.phpfreaks.com/topic/50506-loop-in-mail-function/#findComment-248141
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.