【问题标题】:phpmailer looping through multiple emails [PHP PHPMAILER]phpmailer 循环遍历多封电子邮件 [PHP PHPMAILER]
【发布时间】:2016-12-28 05:26:39
【问题描述】:

我正在使用 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';
}  

现在,代码可以运行,但是,当您在电子邮件客户端查看“TO”时,它还会输出其他电子邮件,包括在旁边 - 我不希望这样。就像,当你给 xxx.com 发邮件时,xxx2.com 可以在他的“收件人”字段中看到TO &lt;name1&gt; &lt;xxx.com&gt;, &lt;name2&gt; &lt;xxx2.com&gt;。我该如何避免呢?我应该从$mail = new PHPMailer; 开始创建一个循环吗?

【问题讨论】:

  • 您只是在发送前将电子邮件地址添加到收件人列表中。您还需要将$mail-&gt;send() 放入循环中。
  • 我的代码现在应该是什么样子?
  • 在下面查看我的答案

标签: php phpmailer


【解决方案1】:

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

<?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-&gt;clearAddresses();

【讨论】:

  • $mail-&gt;clearAddresses(); 是我要找的,谢谢!
【解决方案2】:

将他们添加为抄送收件人的更好方法。

$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);
}

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

【讨论】:

  • 密件抄送会更好 - 它不会向所有人公开所有收件人地址。
猜你喜欢
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
  • 2017-06-27
  • 2014-04-14
  • 1970-01-01
相关资源
最近更新 更多