当邮件字段为空但不是必需时,PHPmailer不发送邮件?

时间:2017-11-02 11:11:55

标签: php phpmailer

http://nodeiterator.pl/

为什么我的php邮件程序脚本在邮件字段为空但不是必需时不发送邮件?我收到消息“请稍后再试”。我错过了什么?

这是我的剧本:

$msg = "";
use PHPMailer\PHPMailer\PHPMailer;
include_once "phpmailer/src/PHPMailer.php";
include_once "phpmailer/src/Exception.php";


if (isset($_POST['submit'])) {
    $subject = $_POST['subject'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != "") {
        $file = "attachment/" . basename($_FILES['attachment']['name']);
        move_uploaded_file($_FILES['attachment']['tmp_name'], $file);
    } else
        $file = "";

    $mail = new PHPMailer();



    $mail->addAddress('piterdeja@gmail.com');
    $mail->setFrom($email);
    $mail->Subject = $subject;
    $mail->isHTML(true);
    $mail->Body = $message;
    $mail->addAttachment($file);

    if ($mail->send())
        $msg = "Your email has been sent, thank you!";
    else
        $msg = "Please try again!";


}

2 个答案:

答案 0 :(得分:1)

我不认为默认情况下PHPMail会让你发送一个空身的电子邮件,但你可以去:

$mail->AllowEmpty = true;

答案 1 :(得分:0)

检查返回的错误:

if(!$mail->Send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

通常phpmailer不接受空体,你必须使用属性$mailer->AllowEmpty = true;强制它