如果PHPMailer失败,则回退到本机PHP邮件功能
发布时间:2020-12-13 16:57:01 所属栏目:PHP教程 来源:网络整理
导读:各种用例示例中的 PHPMailer documentation表明代码的最后部分 – 发送 – 如果失败则应显示错误消息: if(!$mail-send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail-ErrorInfo;} else { echo 'Message has been sent';} 在发生
各种用例示例中的
PHPMailer documentation表明代码的最后部分 – 发送 – 如果失败则应显示错误消息:
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语句中,!$mail-> send()部分已经尝试发送邮件.所以在你的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,$message); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |