Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果PHPMailer失败,则返回到本机PHP邮件函数_Php_Phpmailer - Fatal编程技术网

如果PHPMailer失败,则返回到本机PHP邮件函数

如果PHPMailer失败,则返回到本机PHP邮件函数,php,phpmailer,Php,Phpmailer,在其各种用例示例中,建议代码的最后一部分(send)在失败时显示错误消息: if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } 如果出现故障,我更愿意做的是提供对本机PHP mail()函数的回退。这样做的好处是,电子邮件仍然会发送给我

在其各种用例示例中,建议代码的最后一部分(send)在失败时显示错误消息:

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
如果出现故障,我更愿意做的是提供对本机PHP mail()函数的回退。这样做的好处是,电子邮件仍然会发送给我,并且它可以携带PHPMailer失败的通知

// If PHPMailer code library fails (for whatever reason), fallback to mail()
if (!mail->send()) {

     //  just an example
     $to      = 'me@mydomain.com';
     $subject = 'You have a new e-mail subscriber + ERROR REPORT';
     $message = 'user input data and error description here';
     mail($to, $subject, $message);

} else {

     // other settings defined earlier in script
     $mail->Subject = 'You have a new e-mail subscriber';
     $mail->Body = $message;    
     $mail->send();

}
我不太愿意使用PHPMailer语言作为
IF
表达式,因为对我来说,完全回退到PHP似乎更安全、更可靠。有没有更好的方法写这个


编辑:根据下面的评论,这里有更多的澄清。默认情况下,PHPMailer使用
mail()
。所以这个问题不适用于默认场景。但是,我的实现总是使用自定义SMTP设置(例如SMTP.google.com),这需要用户名和密码。我的问题涉及到定制邮件服务器、用户名或密码出现问题的可能性。因此,除了屏幕上的简单错误消息之外,我正在寻找一种回退解决方案。

您应该首先为PHPMailer分配任务。在if语句中,
$邮件->发送()
部件已尝试发送邮件。因此,在您的
else
语句中,实际上您使用PHPMailer再次发送邮件。您可以按如下方式使用。它将尝试使用PHPMailer发送邮件,如果失败,它将使用PHP的邮件功能

 $mail->Subject = 'You have a new e-mail subscriber';
 $mail->Body = $message;    
 if (!$mail->send()) { 
     $to      = 'me@mydomain.com';
     $subject = 'You have a new e-mail subscriber + ERROR REPORT';
     $message = 'user input data and error description here';
     mail($to, $subject, $message);
 }

除我的评论外,以下是您如何在PHPMailer中从使用SMTP传输退回到使用
mail()
传输的方法:

...
$mail->isSMTP();
if (!$mail->send()) {
    //Fall back to using mail()
    $mail->isMail();
    if (!$mail->send()) {
        echo 'Message failed to send';
    } else {
        echo 'Message sent via mail()';
    }
} else {
    echo 'Message sent via SMTP';
}

通过在
if
语句中使用
mail->send()
,然后在
else
正文中再次使用,您可能会成功发送两次电子邮件?是的,我同意。我发布的代码主要是为了说明。我想应该有更好的方法来解决这个问题。这个问题毫无意义。默认情况下,PHPMailer调用
mail()
本身,因此这根本不是一种回退。如果您使用的SMTP传输失败,您可以再次调用
isMail()
send()
,它将完全按照您选择的答案执行,但不会出现与您自己调用
mail()
相关的无数问题。@Synchro,我不确定您的评论是否准确。“默认情况下,PHPMailer调用
mail()
自身”。。。对这是正确的。如果我使用默认设置,我就不需要问这个问题。但是,无论我在哪里使用PHPMailer,我都会使用自定义SMTP设置(例如SMTP.google.com),这需要用户名和密码。我的问题涉及到定制邮件服务器、用户名或密码出现问题的可能性。因此,除了在屏幕上显示一条简单的错误消息之外,我一直在寻找一个回退解决方案。当然,这就是为什么我使用PHPMailer发布了我的答案。