PHPMailer拒绝发送没有附件的电子邮件

我正在使用 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>


一只斗牛犬
浏览 198回答 3
3回答

蝴蝶刀刀

我会重写它,变成这样:&nbsp; &nbsp; // Now create a message&nbsp; &nbsp; require 'vendor/autoload.php';&nbsp; &nbsp; //$mail->AllowEmpty = true;&nbsp; &nbsp; $mail = new PHPMailer;&nbsp; &nbsp; $mail->setFrom('info@*****.com', 'first last');&nbsp; &nbsp; $mail->addAddress('info@*****.com');&nbsp; &nbsp; $mail->Subject = 'PHPMailer file sender';&nbsp; &nbsp; $mail->Body = "Contact form submission:\n" . "Name: $sender_name\n";&nbsp; &nbsp; if (array_key_exists('userfile', $_FILES) && $_FILES["userfile"]["error"] == UPLOAD_ERR_OK) {&nbsp; &nbsp; &nbsp; &nbsp; // First handle the upload&nbsp; &nbsp; &nbsp; &nbsp; // Don't trust provided filename - same goes for MIME types&nbsp; &nbsp; &nbsp; &nbsp; // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation&nbsp; &nbsp; &nbsp; &nbsp; $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name']));&nbsp; &nbsp; &nbsp; &nbsp; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Upload handled successfully&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Attach the uploaded file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //$mail->addAttachment($uploadfile, 'My uploaded file');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name = $_FILES['userfile']['name'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ext = end((explode(".", $name)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->addAttachment($uploadfile, 'You Have Attachment.'.$ext);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $msg .= 'Failed to move file to ' . $uploadfile;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!$mail->send()) {&nbsp; &nbsp; &nbsp; &nbsp; $msg .= "Mailer Error: " . $mail->ErrorInfo;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $msg .= "Message sent!";&nbsp; &nbsp; }这样它将发送电子邮件,如果有文件附件,它将被包含在内。

largeQ

它以这种方式工作,现在它正在发送带/不带附件的电子邮件,我还将发件人姓名和电子邮件等许多内容更改为变量和正文内容。现在我没有收到任何 PHP 错误,并且表单按照我的意愿完美运行,我希望这会对某人有所帮助,谢谢大家。<?phpuse PHPMailer\PHPMailer\PHPMailer;$msg = '';if (array_key_exists('userfile', $_FILES)) {&nbsp; &nbsp; require 'vendor/autoload.php';&nbsp; &nbsp; // Create a message&nbsp; &nbsp; $mail = new PHPMailer;&nbsp; &nbsp; $mail->SetFrom($_POST['email'], $_POST['sender_name']); //altered&nbsp; &nbsp; $mail->addAddress('info@***.com');&nbsp; &nbsp; $mail->Subject = 'Service Request';&nbsp; &nbsp; $mail->Body = "Sender: " .$_POST['sender_name'] ."\n" ."Phone: " .$_POST['phone']; //altered&nbsp; &nbsp; //Attach multiple files one by one&nbsp; &nbsp; for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {&nbsp; &nbsp; &nbsp;$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));&nbsp; &nbsp; &nbsp;$filename = $_FILES['userfile']['name'][$ct];&nbsp; &nbsp; &nbsp; &nbsp;if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->addAttachment($uploadfile, $filename);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; if (!$mail->send()) {&nbsp; &nbsp; &nbsp; &nbsp; $msg .= "Mailer Error: " . $mail->ErrorInfo;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $msg .= "Message sent!";&nbsp; &nbsp; }}?>

湖上湖

完成实际发送的整个脚本取决于是否有文件是或否的事实。这是由脚本顶部的 if 语句处理的。我建议在始终发送电子邮件的地方进行编辑,并且只有在有文件是或否时才会添加文件。但由于我在移动设备上,我不知道编辑建议是否通过。
打开App,查看更多内容
随时随地看视频慕课网APP