Jump to content

Loop in Mail Function


PHPNS

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.