2

Currently I am working on a PHP email script using PHPMailer` library. I am sending a mass mail using BCC for all the email addresses.

I want each email to contain the current recipient's email address in the message body.

Below is my sample code:

<?php
 require 'PHPMailerAutoload.php';
 $mail = new PHPMailer;
 $mail->isSMTP();                                      
 $mail->Host = 'smtp1.example.com;smtp2.example.com';  
 $mail->SMTPAuth = true;                                
 $mail->Username = 'user@example.com';                 
 $mail->Password = 'secret';                           
 $mail->SMTPSecure = 'tls';                         
 $mail->Port = 587;                               
 $mail->setFrom('from@example.com', 'Mailer');
 $mail->addAddress('noreply@example.com');

 $arrMail [] = array('bcc1@example.com','bcc2@example.com'); 

 for($i=0;$i<count( $arrMail);$i++)
 {
     $mail->addBCC($arrMail[$i]);
     $htmlversion = 'Hello '.$arrMail[$i].' !'.
 }


//  $htmlversion = 'Hello <email_id needed here> !'.

  $mail->Body    = $htmlversion;
  $mail->AltBody = $textVersion;   

      if(!$mail->send()) 
      {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
      }
      else
      {
        echo 'Mail sent';
      } 

Problem: If bcc1@example.com receives the email, its message body should contain their email address. Currently I am getting the first email address in the message body for every recipient.

Note: I dont want to send mail one-by-one using To like mentioned in other pages.

Also is it possible by using some session or database logic?

I am using php 5.5.9.

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
Hiranya Sarma
  • 1,454
  • 4
  • 15
  • 25

1 Answers1

0

Your code is reusing the same email address because you didn't put the creation of the mail body in the loop. If you use a loop then you also don't need BCC.

$arrMail [] = array('bcc1@example.com', 'bcc2@example.com'); 
$total = count($arrMail);

for($i = 0; $i < $total; $i++) {

    $email = $arrMail[$i];
    $htmlversion = "Hello $email !";

    $mail->Body = $htmlversion;
    $mail->AltBody = $textVersion;
    $mail->AddAddress($email);

    if (!$mail->send()) {
       echo 'Message could not be sent.';
       echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
       echo 'Mail sent';
    }
}

I dont want to send mail one-by-one using To like mentioned in other pages.

Unfortunately, BCC by its very nature sends the same email to multiple recipients. If you want to customise each email for each person, you have to send them individual emails.

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
  • The reason I am using `bcc`, so that one recipient cant see othere recipient's mail id. Also what about `Cc`? – Hiranya Sarma Oct 03 '16 at 11:00
  • @Hiranya Whether you want to or not, it's impossible to use BCC or CC if you want to change the email body for each recipient. The whole point of BCC or CC means *copy* the same email to each person. – BadHorsie Oct 03 '16 at 11:26