phpmailer循环访问多个电子邮件[PHP PHPMAILER]

时间:2016-12-28 05:26:39

标签: php phpmailer

我正在使用phpmailer,我想通过使用for循环将其一次发送到多个电子邮件。这是我目前的编码:

require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;                       
$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 = 'yyy.com';                 // SMTP username
$mail->Password = 'yyy';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
$person = array(0 => 'xxx.com', 'xxx2.com');
for ($x = 0; $x < 2; $x++) {
$mail->addAddress($person[$x]); 
}
$mail->setFrom('yyy.com'); 
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = "xxx";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

现在,代码有效,但是,当您在电子邮件客户端检查“收件人”时,它还会输出包含在内的其他电子邮件 - 我不希望这样。就像,当您通过电子邮件发送到xxx.com时,xxx2.com可以在他的“TO”字段中看到TO <name1> <xxx.com>, <name2> <xxx2.com>。我该如何避免?我应该从$mail = new PHPMailer;开始创建一个循环吗?

2 个答案:

答案 0 :(得分:0)

将它们添加为Carbon Copy收件人的更好方法。

$mail->AddCC('person1@domain.com', 'Person One');
$mail->AddCC('person2@domain.com', 'Person Two');
// ..

为了简单起见,你应该循环一个数组来做到这一点。

$recipients = array(
   'person1@domain.com' => 'Person One',
   'person2@domain.com' => 'Person Two',
   // ..
);
foreach($recipients as $email => $name)
{
   $mail->AddCC($email, $name);
}

我没有实现这个,但我认为这对你有用。

答案 1 :(得分:0)

这是另一种可能的方法,无需每次都重新创建类。

<?php

require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 = 'yyy.com';                 // SMTP username
$mail->Password = 'yyy';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
$person = array(0 => 'xxx.com', 'xxx2.com');

$mail->setFrom('yyy.com');
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = "xxx";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$errors = [];
for ($x = 0; $x < 2; $x++) {
    $mail->addAddress($person[$x]);
    if(!$mail->send()) {
        $errors[] = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
    }
    $mail->clearAddresses();
}

if(empty($errors)) {
    // success
} else {
    // handle errors
}

在循环中使用$mail->clearAddresses();