我有一个HTML表单,它将索引传递给另一个表单,这会创建必要的变量,然后将这些变量插入到PHPMailer中。其中一个选项是浏览到随后附加到电子邮件的文件。 我遇到的问题是,如果没有要附加的文件,(然后$ file1变量具有“无值”)PHPMailer返回消息无法发送的内容,如下所示 - 指向存储文件的目录。 无法发送消息。邮件程序错误:无法访问文件:C:/ wamp64 / www / CIA / PODS /
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
//$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.smtp2go.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxx@xx-xxxxxx.co.za'; // SMTP username
$mail->Password = 'xxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 2525; // TCP port to connect to
//Recipients
$mail->AllowEmpty = true;
$mail->setFrom('admin@ic-express.co.za', 'ICExpress Waybill Updates');
$mail->addAddress('xxxx@xx.co.za');
$mail->addAddress($email);
$mail->addReplyTo('xxxx@xx.co.za');
$mail->addCC($addemail);
$mail->addCC($addemail2);
echo $file1;
$path=$_SERVER['DOCUMENT_ROOT'].'/CIA/PODS/'.$file1;
$name=$file1;
$mail->AddAttachment($path,$name,$encoding ='base64',$type = 'application/octet-stream');
$mail->AllowEmpty = true;
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject ;
$mail->Body = <<<END
答案 0 :(得分:2)
如果附件可用,您只需添加附件即可。如果没有文件附件,请不要尝试添加附件:
$path = $_SERVER['DOCUMENT_ROOT'].'/CIA/PODS/'.$file1;
if (is_dir($path) === false && file_exists($path)) {
$name = $file1;
$mail->AddAttachment($path, $name, $encoding = 'base64', $type = 'application/octet-stream');
}
正如IncredibleHat在评论中也提到的那样,您也可以使用条件is_file($path)
来检查PHPMailer itself之类的文件路径:
if (is_file($path)) {
$mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/octet-stream');
}
如果您想使用路径中指定的相同内容,也不需要指定name
。如果name
参数是空字符串,PHPMailer正在使用the basename
of the path。