为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
一只斗牛犬 2021-11-26 19:18:22
我正在使用 PHPMailer 发送带有可选附件文件的电子邮件。当发送带有附件的电子邮件存在时,这工作正常。但问题是当附件为空时,PHPmailer 拒绝发送电子邮件,有什么解决办法吗?<?phpuse PHPMailer\PHPMailer\PHPMailer;$msg = '';$sender_name =  $_POST['sender_name'];// Upload handled successfully// Now create a messagerequire '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>
查看完整描述

3 回答

?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

我会重写它,变成这样:


    // 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!";

    }

这样它将发送电子邮件,如果有文件附件,它将被包含在内。


查看完整回答
反对 回复 2021-11-26
?
largeQ

TA贡献2039条经验 获得超7个赞

它以这种方式工作,现在它正在发送带/不带附件的电子邮件,我还将发件人姓名和电子邮件等许多内容更改为变量和正文内容。现在我没有收到任何 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!";

    }

}

?>


查看完整回答
反对 回复 2021-11-26
?
湖上湖

TA贡献2003条经验 获得超2个赞

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


查看完整回答
反对 回复 2021-11-26
  • 3 回答
  • 0 关注
  • 197 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信