Loop thru records, send email needs tweaking

My intention is to loop through my array, compose and send a separate email to each. Instead, the loop composes every email into a single email and sends that to every record. So each person receives an email with everyone else’s email in the body.

[php]while($row = $result->fetch_assoc()){
echo $row[‘FName’]. " - ". $row[‘email’];
echo “
”;

$email = "[email protected]";
$emailto = $row[‘email’];
$subject = “survey”;
$headers = “From: Michelle Hartly <$email>” . “\r\n”;

$body .= " \n "

.$row[‘FName’].",\n". “\n”

."Your subscription is expiring!;

$send = mail($emailto, $subject, $body, $headers);
//sleep(10);
}[/php]

Change:
[php]while($row = $result->fetch_assoc()){[/php]

To:
[php]while($row = $result->fetch_assoc()){
$body = null;[/php]

This will empty the $body variable. You could, rather than the above, remove the concatenate(.) on the $body variable here:
[php]$body .= " \n "[/php]
Thus making the $body variable overwrite itself, rather than appending to itself.

Sponsor our Newsletter | Privacy Policy | Terms of Service