当我过去使用简单文本正文发送不带附件的电子邮件时,出现消息无法发送的错误。邮件程序错误:无法访问文件:./attachment/
如果我评论我的附件功能,我的代码工作正常。
$mail->send 函数每次都会尝试搜索附件文件夹。即使文件不存在于电子邮件中,即文件仅包含文本。
<?php
include('db.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once "vendor/autoload.php";
$id = $_GET['id'];
$query = "select * from access where uid='$id'";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_assoc($result);
$mail = new PHPMailer(true);
try {
$mail->setFrom('sender@gmail.com');
$mail->addAddress('receiver@gmail.com');
$array = explode(", ",$row['attachments']);
$count = count($array);
if($count > 0 && $row['attachments'] != 'null'){
for ($i=0; $i < $count ; $i++) {
$file_to_attach = './attachment/' . $array[$i];
$mail->addAttachment($file_to_attach, $array[$i]);
}
}
$mail->isHTML(true);
$mail->Subject = $row['subject'];
$mail->Body = $row['body'];
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
白衣染霜花
侃侃尔雅