PHPMailer拒绝发送没有附件的电子邮件
Posted
技术标签:
【中文标题】PHPMailer拒绝发送没有附件的电子邮件【英文标题】:PHPMailer refuses to send email without attachment exists 【发布时间】:2019-12-23 22:14:17 【问题描述】:我正在使用 phpMailer 发送带有可选附件文件的电子邮件。 当发送带有附件的电子邮件时,这工作正常。 但是问题是PHPmailer在附件为空时拒绝发送电子邮件,有什么解决办法吗?
<?php
use PHPMailer\PHPMailer\PHPMailer;
$msg = '';
$sender_name = $_POST['sender_name'];
// Upload handled successfully
// Now create a message
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->setFrom('info@*****.com', 'first last');
$mail->addAddress('info@*****.com');
$mail->Subject = 'PHPMailer file sender';
$mail->Body = "Contact form submission:\n" . "Name: $sender_name\n";
if (array_key_exists('userfile', $_FILES))
// First handle the upload
// Don't trust provided filename - same goes for MIME types
// See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
// Attach the uploaded file
//$mail->addAttachment($uploadfile, 'My uploaded file');
$name = $_FILES['userfile']['name'];
$ext = end((explode(".", $name)));
$mail->addAttachment($uploadfile, 'You Have Attachment.'.$ext);
else
$msg .= 'Failed to move file to ' . $uploadfile;
if (!$mail->send())
$msg .= "Mailer Error: " . $mail->ErrorInfo;
else
$msg .= "Message sent!";
?>
这是位于 PHP 代码下的 html 女巫:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHPMailer Upload</title>
</head>
<body>
<?php if (empty($msg)) ?>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>
<input type="text" name="sender_name">
<br>
Send this file: <input name="userfile" type="file">
<br>
<input type="submit" value="Send File">
</form>
<?php else
echo $msg;
?>
</body>
</html>
【问题讨论】:
【参考方案1】:它以这种方式工作,现在它正在发送带/不带附件的电子邮件,我还将发件人姓名和电子邮件等许多内容更改为变量和正文内容。现在我没有收到任何 PHP 错误,并且表单可以按照我的意愿完美运行,我希望这会对某人有所帮助,谢谢大家。
<?php
use PHPMailer\PHPMailer\PHPMailer;
$msg = '';
if (array_key_exists('userfile', $_FILES))
require 'vendor/autoload.php';
// Create a message
$mail = new PHPMailer;
$mail->SetFrom($_POST['email'], $_POST['sender_name']); //altered
$mail->addAddress('info@***.com');
$mail->Subject = 'Service Request';
$mail->Body = "Sender: " .$_POST['sender_name'] ."\n" ."Phone: " .$_POST['phone']; //altered
//Attach multiple files one by one
for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++)
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));
$filename = $_FILES['userfile']['name'][$ct];
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile))
$mail->addAttachment($uploadfile, $filename);
if (!$mail->send())
$msg .= "Mailer Error: " . $mail->ErrorInfo;
else
$msg .= "Message sent!";
?>
【讨论】:
【参考方案2】:我会重写它,变成这样:
// Now create a message
require 'vendor/autoload.php';
//$mail->AllowEmpty = true;
$mail = new PHPMailer;
$mail->setFrom('info@*****.com', 'first last');
$mail->addAddress('info@*****.com');
$mail->Subject = 'PHPMailer file sender';
$mail->Body = "Contact form submission:\n" . "Name: $sender_name\n";
if (array_key_exists('userfile', $_FILES) && $_FILES["userfile"]["error"] == UPLOAD_ERR_OK)
// First handle the upload
// Don't trust provided filename - same goes for MIME types
// See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
// Upload handled successfully
// Attach the uploaded file
//$mail->addAttachment($uploadfile, 'My uploaded file');
$name = $_FILES['userfile']['name'];
$ext = end((explode(".", $name)));
$mail->addAttachment($uploadfile, 'You Have Attachment.'.$ext);
else
$msg .= 'Failed to move file to ' . $uploadfile;
if (!$mail->send())
$msg .= "Mailer Error: " . $mail->ErrorInfo;
else
$msg .= "Message sent!";
这样它会发送Email,如果有文件附件,它会被包含。
【讨论】:
我试过这个,没有附件它什么都不做,根本不发送,但是当我附加文件时它发送的邮件没有附件! @Isabella 我已经编辑了代码,包括-&& $_FILES["userfile"]["error"] == UPLOAD_ERR_OK
-它将检查是否没有错误,文件上传成功。
@Isabella 如果这有效并解决了问题,请选择它作为正确答案!
它以不同的方式工作,感谢您的努力。【参考方案3】:
您完成实际发送的整个脚本取决于是否存在文件的事实。这由脚本顶部的 if 语句处理。我建议在始终发送电子邮件的位置进行编辑,并且只有在文件是或否时才会添加文件。但由于我使用的是移动设备,所以我不知道编辑建议是通过是还是否。
【讨论】:
以上是关于PHPMailer拒绝发送没有附件的电子邮件的主要内容,如果未能解决你的问题,请参考以下文章