2

I'm using php mailer for mail triggering. Its working fine. But I gave 2 to 5 recipients, it sends the mail to only one recipient. In future, I have to trigger a mail to nearly 100 recipients.. I've shared my code below.. Please check it..

 require 'phpmailer/PHPMailerAutoload.php'; 

    $mail = new PHPMailer;

    //$mail->SMTPDebug = 3;                               // Enable verbose debug output

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'karthick****@gmail.com';                 // SMTP username
    $mail->Password = '********';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    $mail->setFrom('karth*******@gmail.com', 'A**n');

    $addresses = explode(',',$emailM);
    foreach ($addresses as $address) {
        $mail->AddAddress($address);
    }


$mail->isHTML(true);                                  

$mail->Subject = 'Need for '.$keyword.'';
$mail->Body    = 'Hi,The Message';


if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Qoute has been sent to all the Manufacturers';
    echo "$address";

}
Arun Karthick
  • 326
  • 2
  • 4
  • 16
  • 1
    Are the email addresses in `$addresses` valid? You should at least add debug message in each foreach-loop cycle. – Raptor Jan 10 '17 at 07:28
  • Are you sure it's only sending to one? You're echoing `$address` at the end, which will only ever contain the last address you added, but the message will still be sent to all of them (though you should check the return value of `addAddress()` to be sure it's working). – Synchro Jan 10 '17 at 07:44
  • I don't think you're going about this in the right way - given the "all the Manufacturers" text at the end, I doubt very much you really want to be adding them all to the same message (where they will all see each other's addresses). You should send a separate message to each one. See [the mailing list example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps) for how to do that efficiently. – Synchro Jan 10 '17 at 07:52
  • What does `$addresses` / `$emailM` look like? – Progrock Jan 10 '17 at 08:22
  • $emailM shows email1@gmail.comemail2@gmail.comemail3@gmail.com.... $address shows email1@gmail.com.. – Arun Karthick Jan 10 '17 at 08:56

2 Answers2

0

A basic idea is that make different connection (object) for each your mailing address like below if You don't have so much addresses in your array.

 require 'phpmailer/PHPMailerAutoload.php'; 

$addresses = explode(',',$emailM);
    foreach ($addresses as $address) {
    $mail = new PHPMailer;

    //$mail->SMTPDebug = 3;                               // Enable verbose debug output

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'karthick****@gmail.com';                 // SMTP username
    $mail->Password = '********';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    $mail->setFrom('karth*******@gmail.com', 'A**n');


        $mail->AddAddress($address);


$mail->isHTML(true);                                  

$mail->Subject = 'Need for '.$keyword.'';
$mail->Body    = 'Hi,The Message';


if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Qoute has been sent to all the Manufacturers';
    echo "$address";

 }

UPDATE :

The second idea is that you can remove recepients each time and add new one and then send like below

require 'phpmailer/PHPMailerAutoload.php'; 

    $mail = new PHPMailer;

    //$mail->SMTPDebug = 3;                               // Enable verbose debug output

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'karthick****@gmail.com';                 // SMTP username
    $mail->Password = '********';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    $mail->setFrom('karth*******@gmail.com', 'A**n');

    $addresses = explode(',',$emailM);
    foreach ($addresses as $address) {

        // for clear last recipients

        $mail->ClearAllRecipients( ) 


        $mail->AddAddress($address);


        $mail->isHTML(true);                                  

           $mail->Subject = 'Need for '.$keyword.'';
           $mail->Body    = 'Hi,The Message';


        if(!$mail->send()) {
          echo 'Message could not be sent.';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
       } else {
           echo 'Qoute has been sent to all the Manufacturers';
          echo "$address";

       }

 }
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
0

You're code looks like it should be doing the trick. Make sure that $address doesn't contain any whitespace for the entries. For safe measure, add the trim() function.

$mail->AddAddress(trim($address));

If that does not work, make sure that you're recipient addresses are real.

Additionally, in case the privacy of the recipients is of concern, I would recommend you use AddBCC() instead of AddAddress() so that their addresses are not revealed.

Rook
  • 141
  • 13