Search code examples
phpemailphpmailer

PHP Mailer Multiple recipients


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";

}

Solution

  • 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.