PHPmailer - 如果附件的变量 $file1 没有值,则发送失败
PHPmailer - failing to send if variable $file1 for attachment does not have a value
我有一个 HTML 表单将索引传递给另一个表单,该表单创建必要的变量,然后将其插入到 PHPMailer 中。其中一个选项是浏览到一个文件,然后将其附加到电子邮件中。
我遇到的问题是,如果没有要附加的文件,(那么 $file1 变量有 "no value")PHPMailer returns 邮件无法发送的 eoor 如下 - 指向目录文件的存储位置。
无法发送消息。邮件程序错误:无法访问 file: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
您可以简单地添加附件(如果有)。如果没有文件附件,请不要尝试添加附件:
$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。
我有一个 HTML 表单将索引传递给另一个表单,该表单创建必要的变量,然后将其插入到 PHPMailer 中。其中一个选项是浏览到一个文件,然后将其附加到电子邮件中。 我遇到的问题是,如果没有要附加的文件,(那么 $file1 变量有 "no value")PHPMailer returns 邮件无法发送的 eoor 如下 - 指向目录文件的存储位置。 无法发送消息。邮件程序错误:无法访问 file: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
您可以简单地添加附件(如果有)。如果没有文件附件,请不要尝试添加附件:
$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。