You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加CC后PHPMailer脚本重复发送邮件问题求助

问题:PHPMailer添加CC后邮件重复发送

我尝试使用PHPMailer发送带有dompdf生成附件的邮件给一组收件人:通过array_shift()获取主收件人并将其从数组中移除,再用foreach遍历剩余邮箱作为CC。但添加CC后邮件会发送两次,无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() // 示例:['main@example.com', 'cc1@example.com']
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文件夹有写入权限

    $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; // 启用详细调试输出
    $mail->isSMTP(); // 设置邮件发送使用SMTP
    $mail->Host = ''; // 指定SMTP服务器
    $mail->SMTPAuth = true; // 启用SMTP认证
    $mail->Username = ''; // SMTP用户名
    $mail->Password = ''; // SMTP密码
    $mail->SMTPSecure = 'tls'; // 启用TLS加密
    $mail->Port = 587; // TCP端口


    $mail->setFrom('', '');
    $mail->addAddress(array_shift($recipients)); // 添加主收件人并从数组移除
    $mail->addReplyTo('');
    foreach($recipients as $ccEmail)
    {
        $mail->AddCC($ccEmail);
    }

    $mail->addStringAttachment($output, 'attachment.pdf');
    
    $mail->isHTML(true); // 设置邮件格式为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}";
}

问题分析与解决思路

可能的核心原因

  1. Dompdf方法调用错误:原代码中$options->getChroot($_SERVER['DOCUMENT_ROOT']);是错误用法,getChroot是获取方法,设置根目录应该用setChroot,这个错误可能导致Dompdf运行异常,间接干扰PHPMailer的执行流程。
  2. PHPMailer方法名不规范:使用大写的AddCC()而非标准驼峰式addCC(),旧版本PHPMailer可能存在兼容问题,导致收件人处理逻辑异常。
  3. 调试日志误判:SMTPDebug = 1会输出详细SMTP交互日志,可能被误认为是重复发送的迹象,实际仅发送一次。

解决步骤

  1. 修复Dompdf配置错误:将getChroot改为setChroot,确保Dompdf正常生成PDF附件。
  2. 规范PHPMailer方法调用:替换AddCC()为标准的addCC()。
  3. 关闭调试模式验证:临时将SMTPDebug设为0,排除日志干扰,确认是否真的重复发送。
  4. 检查收件人列表:添加调试代码打印所有收件人,确认无重复:
    print_r($mail->getAllRecipientAddresses()); exit;
    

修正后的代码

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

// 示例收件人数组
$recipients = ['main@example.com', 'cc1@example.com', 'cc2@example.com'];

try {
    $mail = new PHPMailer(true);

    ob_start();
    include "pdf.php";
    $htmldata = ob_get_clean();
    
    $options = new Dompdf\Options();
    $options->setChroot($_SERVER['DOCUMENT_ROOT']); // 修正为setChroot
    $options->setIsRemoteEnabled(true);
    $options->setIsHtml5ParserEnabled(true);
    $options->setTempDir('temp');

    $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配置
    $mail->SMTPDebug = 0; // 关闭调试模式,避免日志干扰
    $mail->isSMTP();
    $mail->Host = 'smtp.example.com'; // 填写你的SMTP服务器地址
    $mail->SMTPAuth = true;
    $mail->Username = 'your-account@example.com';
    $mail->Password = 'your-password';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    $mail->setFrom('sender@example.com', 'Sender Name');
    $mainRecipient = array_shift($recipients);
    $mail->addAddress($mainRecipient);
    $mail->addReplyTo('reply@example.com');

    // 使用标准addCC方法添加抄送
    foreach($recipients as $ccEmail) {
        $mail->addCC($ccEmail);
    }

    // 调试用:打印所有收件人,确认无重复
    // print_r($mail->getAllRecipientAddresses()); exit;

    $mail->addStringAttachment($output, 'attachment.pdf');
    
    $mail->isHTML(true);
    $mail->Subject = 'Subject ' . rand();
    $mail->Body = '邮件内容';
    $mail->AltBody = '纯文本内容';
    
    $mail->send();
    echo '邮件发送成功';
} catch (Exception $e) {
    echo "邮件发送失败. 错误信息: {$mail->ErrorInfo}";
}

内容的提问来源于stack exchange,提问作者John Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 21:00:58