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

使用PHPMailer发送两封不同邮件仅第二封送达的问题排查

解决PHPMailer发送第一封邮件失败的问题

看了你的代码,第一封邮件没送达的原因主要有几个关键点,咱们一步步来修复:

1. 第一封邮件未做发送错误校验

你调用$mail->send()发送第一封邮件后,完全没判断是否发送成功——哪怕第一封发送失败了,代码还是会继续执行第二封的逻辑,你根本没法第一时间知道第一封出了什么问题。

2. 单引号字符串的换行符未被解析

第一封邮件的Body用单引号包裹,里面的\r\n不会被解析成换行符,会被当成纯文本直接显示,这会导致邮件内容格式混乱,很可能被邮箱服务商判定为垃圾邮件拦截。

3. 复用PHPMailer实例时资源清理不彻底

虽然你用了ClearAddresses()清空收件人,但最好同步清空邮件正文、主题等内容,避免残留第一封邮件的信息干扰第二封,也能保证每封邮件的独立性。

修改后的完整代码示例

/** 
 * This code shows settings to use when sending via Google's Gmail servers. 
 */ 
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require 'PHPMailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "olaozias@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "password";

//Set who the message is to be sent from
$mail->setFrom('olaozias@gmail.com', 'Department of Information Science');

//Set an alternative reply-to address
$mail->addReplyTo('olaozias@gmail.com', 'Department of Information Science');

// --- 第一封邮件:发送给家长 ---
$mail->addAddress($email , 'Parent');
$mail->Subject = 'Student Attendance System';
// 改用双引号包裹,让\r\n解析为换行;或用PHP_EOL适配不同系统换行规则
$mail->Body = "Dear Parent \r\n This email is sent from the university of gondar , Department of information science to inform you that your child ". $firstname." has been registered for semester ".$semister. " in order to see your child attendance status and to communicate easily with our department use our attendance system. First download and install the mobile application which is attached in this email to your phone and use these login credentials to login to the system \r\n Your child Id: ".$student_no. "\r\n Password: ".$parent_pass."\r\n Thank you for using our attendance system \r\n University of Gondar \r\n Department of Information Science ";

// 发送第一封并检查错误
if (!$mail->send()) {
    echo ' <script type = "text/javascript"> alert("第一封邮件发送失败: ' . $mail->ErrorInfo . '"); window.location = "student.php"; </script> ';
    exit; // 发送失败就停止后续执行,避免无效操作
} else {
    // 清理收件人、正文、主题,为第二封邮件做准备
    $mail->clearAddresses();
    $mail->Body = '';
    $mail->Subject = '';
}

// --- 第二封邮件:发送给学生 ---
$mail->AddAddress($stud_email,'Student');
$mail->Subject = 'Student Attendance System';
$mail->Body = "email 2";

//send the message, check for errors
if (!$mail->Send()) {
    echo ' <script type = "text/javascript"> alert("Mailer Error: ' . $mail->ErrorInfo . '"); window.location = "student.php"; </script> ';
} else {
    echo ' <script type = "text/javascript"> alert("student Added successfully and an Email the sent to email address provided"); window.location = "student.php"; </script> ';
}

额外排查建议

  • 如果你用Gmail发件,确保账号已开启低安全性应用访问(更安全的方式是用应用专用密码,尤其是开启两步验证的账号),否则SMTP连接会被拒绝。
  • 检查第一封邮件的收件人邮箱$email是否拼写正确,有没有格式错误。
  • 查看收件人邮箱的垃圾邮件文件夹,第一封邮件可能因格式问题被归类到垃圾邮件中。

内容的提问来源于stack exchange,提问作者Olana Kenea

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:48:07