Skip to content
Advertisement

phpmailer for each loop custom body

I am sending emails using an online form via phpmailer and trying to use a for each loop to customize the body, specifically for an unsubscribe button. I am currently only using two of my personal emails with no encryption for testing purposes. I will add encryption once this actually starts to work as it should.

My php code:

$mail = new PHPMailer;

/*php mailer settings*/
//All settings for php mailer here - working fine, email sends

/*for each loop to send bcc to each email and customize body*/

//array of emails - really loading from database with while loop
$subs_email("email1@example.com","email2@example.com");

foreach ($subs_email as $email) {
$mail->addBCC($email);

$mail->Body = '<p>This is the body text</p><a href="http://www.website.com/unsubscribe.php?email='.$email.'">Unsubscribe</a>';
}
 if(!$mail->send()) {
     echo 'Message could not be sent.';
     echo 'Mailer Error: ' . $mail->ErrorInfo;
 }

Using a variation of the code above the email will send (bcc) to the two emails in the array but all emails will get the same unsubscribe link/email address. Note email1@ is used both times.

Email to email address 1: Body text looks good.

<a href="http://www.website.com/unsubscribe.php?email=email1@example.com">Unsubscribe</a>

Email to email address 2: Body text looks good.

<a href="http://www.website.com/unsubscribe.php?email=email1@example.com">Unsubscribe</a>

This is obviously not what I want. When I do testing and just echo out the for each loop text on a blank page it shows each custom unsubscribe link as it should, one for each email address.

Any help is appreciated, and let me know if you need more or .

working code created based on answer Main issue was using addBCC instead of addAddress

        foreach ($subs_email as $email) {
            $mail->addAddress($email);

            $mail->Body = '<p>This is the body text</p><a href="http://www.website.com/unsubscribe.php?email='.$email.'">Unsubscribe</a>';


            if (!$mail->send()) {
                echo "Mailer Error" . $mail->ErrorInfo . '<br />';
                break; //Abandon sending
            }
            // Clear all addresses and attachments for next loop
            $mail->clearAddresses();
        }

Advertisement

Answer

This is how BCC works – the same message is sent to all recipients. You need to send a separate message to each recipient, as described in the mailing list example provided with PHPMailer.

For efficiency, you should create a single instance before your loop, iterate over your list, while setting the body differently for each message (the code you have is fine, if you want more flexibility perhaps use a templating system), send the message, then clear the recipient list so the next message only gets sent to one address. It also helps to use SMTP keepalive to increase throughput. The example script does most of this.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement