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

PHP使用PHPMailer批量发送邮件报错求助

问题:批量发送邮件失败

我在网站上实现邮件发送功能,想一次性给多个收件人发邮件。表单里做了多选字段,选单个收件人时邮件能正常发送,但选多个时出现报错:

"2023-04-01 18:12:52 Invalid address: praisekayenda.pk@example.com,praisewalker88@example.com 2023-04-01 18:12:52 Invalid address: Message could not be sent.Mailer Error: You must provide at least one recipient email address."

我使用的代码
if (isset($_POST['submit'])) {

    $to_array = $_POST['to'];
    $cc  = mysqli_real_escape_string($conn, $_POST['cc']);
    $title = mysqli_real_escape_string($conn, $_POST['subject']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);

    $send_email = implode(",", $to_array);
    $subject = $title;
    $comment = $message;

    $mail = new PHPMailer;
    $mail->SMTPDebug = 2;                             
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'xulprojectmust@example.com';
    $mail->Password = 'xxx';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;
    $mail->setFrom('xulprojectmust@example.com', 'MUST');
    foreach ($to_array as $to) {
        $mail->addAddress($to);
    }
    $mail->addReplyTo('xulprojectmust@example.com');

    // Add CC recipients
    $ccRecipients = explode(",", $cc);
    foreach ($ccRecipients as $ccRecipient) {
        $mail->addCC(trim($ccRecipient));
    }

    $mail->isHTML(true);
    $mail->Subject = $subject;
    $mail->Body    = $comment;
    $mail->AltBody = $send_email;
    $mail->addCustomHeader("To: " . $send_email);

    if (!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
        echo "<script>window.alert(\" failed to send emails $mail->ErrorInfo\")</script>";
    } else {

        echo 'Message sent';

    }
}
问题原因与修复方案

核心错误点

  1. 自定义To头冲突:你手动添加的addCustomHeader("To: " . $send_email)会覆盖PHPMailer通过addAddress()自动生成的合规To头。PHPMailer验证收件人时会检查这个自定义头的格式,直接用逗号拼接的字符串不符合邮件头规范,导致被判定为无效地址。
  2. AltBody设置错误:AltBody是邮件的纯文本备用内容,你把它设成收件人列表,逻辑错误且可能干扰发送流程。

修复后的代码

if (isset($_POST['submit'])) {
    $to_array = $_POST['to'];
    $cc = mysqli_real_escape_string($conn, $_POST['cc']);
    $title = mysqli_real_escape_string($conn, $_POST['subject']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);

    $subject = $title;
    $comment = $message;

    $mail = new PHPMailer;
    $mail->SMTPDebug = 2;                             
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'xulprojectmust@example.com';
    $mail->Password = 'xxx';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;
    $mail->setFrom('xulprojectmust@example.com', 'MUST');
    
    // 批量添加收件人(保留原有正确逻辑)
    foreach ($to_array as $to) {
        // 新增:验证邮箱格式,过滤无效地址
        if (filter_var($to, FILTER_VALIDATE_EMAIL)) {
            $mail->addAddress($to);
        }
    }
    
    $mail->addReplyTo('xulprojectmust@example.com');

    // 新增:处理空CC字段,避免无效循环
    if (!empty($cc)) {
        $ccRecipients = explode(",", $cc);
        foreach ($ccRecipients as $ccRecipient) {
            $trimmed_cc = trim($ccRecipient);
            if (filter_var($trimmed_cc, FILTER_VALIDATE_EMAIL)) {
                $mail->addCC($trimmed_cc);
            }
        }
    }

    $mail->isHTML(true);
    $mail->Subject = $subject;
    $mail->Body = $comment;
    // 修复:设置正确的纯文本备用内容
    $mail->AltBody = strip_tags($comment);

    // 删除错误的自定义To头,让PHPMailer自动处理
    // $mail->addCustomHeader("To: " . $send_email);

    if (!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
        // 修复:转义弹窗内容中的特殊字符,避免JS报错
        echo "<script>window.alert('Failed to send emails: " . addslashes($mail->ErrorInfo) . "');</script>";
    } else {
        echo 'Message sent';
    }
}

额外优化建议

  • 始终验证邮箱格式:用filter_var()过滤无效地址,避免单个错误地址导致整批邮件发送失败。
  • 处理空字段逻辑:对CC等可选字段做非空判断,减少无效操作。
  • 转义前端输出内容:避免特殊字符破坏JS或HTML结构。

内容的提问来源于stack exchange,提问作者Praise Kayenda

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 15:24:55