Jump to content

PhpMailer is sending the same body in loop


coldfission

Recommended Posts

The code below is supposed to send each recipient a different email body, but the same email body from the first item is being sent to everybody in the list. How do i get the body of the email to be reset for each email?

 

$query = "SELECT orders_id, customers_name, customers_email_address FROM orders WHERE date_purchased LIKE '%$past_date%' AND orders_status=3";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

require_once "phpmailer/class.phpmailer.php";
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->IsHTML(true);
$mail->Host = "localhost.localdomain"; // SMTP server	
$mail->From = "[email protected]";
$mail->FromName = "TackWholesale.com";
$mail->AddReplyTo("[email protected]", "Tackwholesale.com");
$mail->Subject = "Review your Purchased Items and take our Survey";

while ($row = mysql_fetch_array($result))
{
$orders_id = $row['orders_id'];
$enc_orders_id = ($orders_id*897)-411;
$customers_email_address = $row['customers_email_address'];
$customers_name = $row['customers_name'];
$customers_name = explode(" ",$customers_name);
$customers_name = $customers_name[0];

$feedback_email = str_replace('past_date', $past_date, $feedback_email);
$feedback_email = str_replace('customers_name', $customers_name, $feedback_email);
$feedback_email = str_replace('enc_orders_id', $enc_orders_id, $feedback_email);

$mail->Body = $feedback_email;

$mail->AddAddress($customers_email_address);

if(!$mail->Send())
{
	echo "error";
}

$mail->ClearAddresses();
}

you need to copy $feedback_email because at the start of the first loop all the replaces take place but then they cant take place next loop because they have all ready been replaced change your code to

        $new_email = $feedback_email;
$new_email = str_replace('past_date', $past_date, $new_email);
$new_email = str_replace('customers_name', $customers_name, $new_email);
$new_email = str_replace('enc_orders_id', $enc_orders_id, $new_email);

$mail->Body = $new_email;

 

Scott.

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.