How to Reset the Array of Mail Recipients using PHPMailer

By | July 22, 2012

Today I had to fix a script that sends an e-mail using PHPMailer. Here is the intital code:

$mail->From = "la******@gm***.com";
$mail->FromName = "Lampdocs.com";
$mail->AddAddress("te**@la******.com");
$mail->AddReplyTo($webmaster_email, "Lampdocs.com");
$mail->WordWrap = 70; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Order from New Paypal Address";
$mail->Body = $milo; //HTML Body
$mail->Send();

I had to send 2 different mails: one to the buyer and another one to administrator. If I tried to send another mail by using the same code, I got 2 recipients for e-mail. I’ve searched for the way in documentation, but the best way was to check the code of the class itself. Here is the string you should add before sending the next e-mail:

$mail->ClearAllRecipients();

This will clear all arrays with recipients. You can now send another e-mail, specifying your next recipient and a copy will not be sent to the first one.