PHP邮件程序多个收件人

时间:2017-01-10 07:24:28

标签: php email phpmailer

我正在使用php邮件程序进行邮件触发。 它的工作正常。但我给了2到5个收件人,它只向一个收件人发送邮件。将来,我必须向近100名收件人发送邮件.. 我在下面分享了我的代码..请检查一下..

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

}

2 个答案:

答案 0 :(得分:0)

您的代码看起来应该是这样做的。确保$address不包含条目的任何空格。为安全起见,请添加trim() 功能

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

如果不起作用,请确保您的收件人地址是真实的。

此外,如果收件人的隐私受到关注,我建议您使用 AddBCC() 而不是 AddAddress() ,以便他们的地址不透露。

答案 1 :(得分:-1)

一个基本的想法是为每个邮寄地址制作不同的连接(对象),如下所示如果您的阵列中没有这么多地址。

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

 }

更新:

第二个想法是,您可以每次删除收件人并添加新收件人,然后发送如下

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

       }

 }