61

There are a lot of StackOverflow questions on this topic, but I couldn't find one that was able to help with the issue I'm having. The script that I'm writing sends out multiple emails to various recipients with different message contents.

I can get this working by re-initializing the phpMailer object multiple times, but what I'd like to be able to do is create the object a single time, and then re-assign the following fields:

$mail->AddAddress($email);
$mail->Subject = $subject;
$mail->IsHTML(false);
$mail->Body = $message;

That way I can just run those four lines of code and then send the mail out, again and again, as many times as necessary. The Subject, IsHTML, and Body fields are easily changed, so the problem I'm having is in the AddAddress function.

As you can probably guess, after I send out the first email, changing recipients for future emails will result in those stacking onto the current list of recipients.

To put it simply, how can I remove the email addresses associated with my $mail object so that I can assign them each time while removing the old addresses?

Is there another function besides AddAddress that I can use that will just assign the addresses?

2
  • How about sticking the rest of the initializing in a function? Then you could create an almost initiated instance easily and set the rest of the fields.
    – Svish
    Commented Jun 8, 2012 at 16:19
  • 1
    That's actually what I'm doing. I have an init_mailer function which creates the object and assigns all the options except for those four lines I posted above. I'm trying to optimize this as best as possible. Commented Jun 8, 2012 at 16:29

2 Answers 2

129

You can use clearAllRecipients( )

$mailer->clearAllRecipients( ); // clear all

1
38

im using this always before sending email to recipients:

// clear addresses of all types
$mail->ClearAddresses();  // each AddAddress add to list
$mail->ClearCCs();
$mail->ClearBCCs();

then im doing just this: (not using CC or BCC, $toaddress is just an array of recipients)

foreach($toaddress as $key=>$val) { $mail->AddAddress( $val ); }

im using PHPMailer 5.2

4
  • 2
    So judging from the post above, the ClearAllRecipients function will remove Addresses, CCs, and BCCs, while the functions you listed will remove only those addresses in the specified field. Calling all three of those functions will have the same effect as ClearAllRecipients, if I understand correctly. Thank you very much for your help. Commented Jun 8, 2012 at 16:32
  • 2
    yes this will have pretty much the same effect, but i want to call them this way so to apply selective removal (sometimes)
    – user1299518
    Commented Jun 8, 2012 at 16:45
  • 2
    Of course. I'm not intending on using any CCs or BCCs in my script, so I will just be using the ClearAddresses function for efficiency. Commented Jun 8, 2012 at 20:07
  • 1
    Thank you, this worked perfectly for my needs. Cheers!
    – user752746
    Commented Jan 22, 2020 at 18:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.