【问题标题】:PHPMailer script sending email twice when CC added添加 CC 时,PHPMailer 脚本会发送两次电子邮件
【发布时间】:2023-01-20 18:31:28
【问题描述】:

我正在尝试将包含使用 dompdf 生成的附件的电子邮件发送到收件人列表。使用array_shift() 获取主要电子邮件并将其从数组中删除,并使用foreach 将所有其他电子邮件作为抄送。当我添加 CC 时,电子邮件被发送了两次。如果没有抄送,则只发送一次。即使 CC 为空,也会发送两次。如果我删除那行AddCC(),它将只发送一次。

我可以寻求任何想法或帮助吗?

require_once 'dompdf/autoload.inc.php';
require_once 'phpqrcode/qrlib.php';

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);

$recipients = array() // array of emails to be sent
try {

    ob_start();
    include "pdf.php";
    $htmldata = ob_get_clean();
    
    $options = new Dompdf\Options();
    $options->getChroot($_SERVER['DOCUMENT_ROOT']);
    $options->setisRemoteEnabled(true);
    $options->setIsHtml5ParserEnabled(true);
    $options->setTempDir('temp'); // temp folder with write permission

    $dompdf = new Dompdf\Dompdf($options);
    
    $dompdf->loadHtml($htmldata);
    
    $customPaper = array(0,0,600,600);
    $dompdf->set_paper($customPaper);
    
    QRcode::png($data, "qr.png", "L", 4, 4);
    
    $dompdf->render();
    $output = $dompdf->output();

    //Server settings
    $mail->SMTPDebug = 1; // Enable verbose debug output
    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host = ''; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true; // Enable SMTP authentication
    $mail->Username = ''; // 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('', '');
    $mail->addAddress(array_shift($recipients)); // Add first email as the recipient and remove the email from array
    $mail->addReplyTo('');
    foreach($recipients as $ccEmail)
    {
        $mail->AddCC($ccEmail);
    }

    $mail->addStringAttachment($output, 'attachment.pdf');
    
    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject = 'Subject'.rand();
    $mail->Body = 'Content';
    $mail->AltBody = 'content';
    
    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

【问题讨论】:

  • 良好的代码缩进将帮助我们阅读代码,更重要的是,它将有助于你调试你的代码Take a quick look at a coding standard 为了您自己的利益。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我。

标签: php email duplicates smtp phpmailer


【解决方案1】:

看起来问题出在将抄送收件人添加到电子邮件的 foreach 循环中。 问题是当你使用

$mail->addAddress(array_shift($recipients))

要设置主要收件人,它还会将电子邮件发送给 $recipients 数组中的第一个收件人。然后当 foreach 循环运行时,它将把每个剩余的收件人添加为抄送,导致电子邮件被发送给每个收件人两次。

您可以尝试通过在 addAddress() 调用之前移动添加抄送收件人的 foreach 循环来解决此问题,如下所示:

foreach($recipients as $ccEmail)
{
   $mail->AddCC($ccEmail);
}
$mail->addAddress(array_shift($recipients));

【讨论】:

  • 这将抄送给所有收件人,然后再次作为收件人发送:收件人列表中的第一个人
猜你喜欢
  • 1970-01-01
  • 2018-06-17
  • 2014-04-14
  • 1970-01-01
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
相关资源
最近更新 更多