
在使用 phpmailer 发送带有附件的邮件时,如果遇到首次尝试失败但页面刷新后成功的现象,这通常指向一个经典的时序问题。在 php 脚本中,当您在同一个请求周期内执行文件生成(如图片、pdf等)和邮件发送任务时,phpmailer 尝试附加文件时,该文件可能尚未完全写入磁盘。这导致 phpmailer 报告“无法访问文件”的错误,因为在文件系统层面上,指定路径的文件暂时不存在或不可读。
当用户刷新页面时,通常会再次触发整个脚本的执行。此时,由于第一次请求可能已经完成了文件的生成和保存,第二次请求(刷新)时文件已经稳定地存在于磁盘上,PHPMailer 就能成功找到并附加文件。
原始代码结构中存在的问题是,PHPMailer 的邮件发送逻辑与证书生成逻辑并行或甚至在证书生成完成之前执行。具体来说,$mail->AddAttachment() 方法被调用时,由 imagepng($createimage,$output,3) 生成的证书文件可能还没有完全写入到 CSD-Certificates/saved-certs/ 目录下。
解决此问题的关键在于确保 PHPMailer 尝试附加文件时,文件已确保存储在指定路径。这意味着文件生成和保存的代码必须在邮件发送代码之前执行。
以下是优化后的逻辑流程:
LimeSurvey是一款问卷调查管理系统,具有问卷的设计、修改、发布、回收和统计等多项功能,集成了调查程序开发、调查问卷的发布以及数据收集等功能,使用它,用户不必了解这些功能的编程细节。 LimeSurvey 3.14.2 中文版 更新日志:2018-08-07 -修正问题#13878:向用户组发送电子邮件-显示问题; -修正问题#13902:LimeSurvey尝试在编辑问题时更新响
154
立即学习“PHP免费学习笔记(深入)”;
为了清晰展示这种依赖关系,我们将原始代码中用于生成证书的部分和 PHPMailer 的邮件发送部分进行逻辑上的重新排序。
<?php
include 'settings.php'; // 包含设置文件
// 导入 PHPMailer 类
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// 加载 Composer 的自动加载器或手动引入文件
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
require 'PHPMailer-master/src/Exception.php';
if (isset($_POST['generate'])) {
$name = ucwords($_POST['name']);
$customerref = ($_POST['customerref']);
$date = ($_POST['date']);
$customeremail = ($_POST['customeremail']);
$weight = ucwords($_POST['weight']);
// 输入验证
if (empty($name) || empty($weight) || empty($date) || empty($customeremail) || empty($customerref)) {
echo "<div class='alert alert-danger col-sm-6' role='alert'>并非所有表单字段都已填写。请重试。</div>";
} else {
// --------------------------------------------------------
// 第一步:生成并保存证书文件
// --------------------------------------------------------
$image = "CSD-Certificates/certi.png";
$createimage = imagecreatefrompng($image);
$output = dirname(__FILE__)."/CSD-Certificates/saved-certs/destruction-cert($customerref-$date).png"; // 确保使用完整路径
// 颜色定义
$white = imagecolorallocate($createimage, 254, 254, 254);
$black = imagecolorallocate($createimage, 0, 0, 0);
$rotation = 0;
$drFont = "CSD-Certificates/TitilliumWeb-Regular.ttf";
// 根据名称长度调整字体大小和位置 (此处省略了完整的逻辑,仅作示意)
$name_len = strlen($name);
$font_size = 50; // 示例值
$origin_x = 1600; // 示例值
$origin_y = 700; // 示例值
// 在图片上绘制文本
imagettftext($createimage, $font_size, $rotation, $origin_x, $origin_y, $white, $drFont, $name);
imagettftext($createimage, 50, $rotation, 2300, 900, $white, $drFont, $weight);
imagettftext($createimage, $font_size, $rotation, 1850, 900, $white, $drFont, $date);
imagettftext($createimage, $font_size, $rotation, 2200, 1980, $white, $drFont, $date);
imagettftext($createimage, $font_size, $rotation, 2200, 2180, $white, $drFont, $customerref);
// 将图片保存到文件系统
imagepng($createimage, $output, 3);
imagedestroy($createimage); // 释放内存
// --------------------------------------------------------
// 第二步:发送邮件(在证书文件保存后执行)
// --------------------------------------------------------
$mail = new PHPMailer(true); // 启用异常
try {
// 服务器设置
$mail->isSMTP();
$mail->Host = 'mail.smtp2go.com';
$mail->SMTPAuth = true;
$mail->Username = 'refurbsa.com';
$mail->Password = 'Y2F6ejMxbGFseTUw';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
// 收件人
$mail->setFrom('sender@example.com', 'Electronic Cemetery');
$mail->addAddress($customeremail);
$mail->addReplyTo('replyto@example.com', 'Electronic Cemetery');
// 添加附件 (现在文件应该已经存在了)
$mail->AddAttachment($output); // 使用之前保存文件的完整路径
// 内容
$mail->isHTML(true);
$mail->Subject = 'Your E-Waste Disposal Certificate';
$mail->Body = "Good day $name,<br><br>
Thank you very much for making use of our services. Your collection has been processed and I have attached your destruction certificate to this email.
<br><br>
If you were happy with our service then it would be very much appreciated if you would spare a moment to give us your review <a href='https://www.facebook.com/eastcoastewaste/reviews'>HERE</a>
<br><br>
We look forward to assisting you with all your e-Waste needs in the future.
<br><br>
Wishing you a wonderful day further!
<br><br>
The Electronic Cemetery Team";
$mail->send();
echo "<div class='alert alert-success col-sm-6' role='alert'>恭喜!$name 的证书已生成并发送到 $customeremail。</div>";
} catch (Exception $e) {
echo "消息无法发送。邮件错误:{$mail->ErrorInfo}";
}
}
}
?>通过以上调整,确保了证书文件在 PHPMailer 尝试附加之前已经完全生成并保存,从而解决了附件首次发送失败的问题。
以上就是解决 PHPMailer 附件发送失败:文件生成与邮件发送的时序问题的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号