PHPNS Posted May 8, 2007 Share Posted May 8, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/50506-loop-in-mail-function/ Share on other sites More sharing options...
soycharliente Posted May 8, 2007 Share Posted May 8, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/50506-loop-in-mail-function/#findComment-248141 Share on other sites More sharing options...
PHPNS Posted May 8, 2007 Author Share Posted May 8, 2007 It helps a lot! Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/50506-loop-in-mail-function/#findComment-248351 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.