本文介绍了如何捕获由 mail() 引起的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何在 php 中捕获邮件错误(发送电子邮件时显示错误并且该错误是由邮件服务器关闭引起的)?

Does anyone know how can I catch a mail error (error is displayed while sending email and the error is caused by the mailserver down) in php?

由电子邮件服务器停机引起的错误如下:

Error that was caused by emailserver down is below:

<!--2010-02-24T14:26:43+11:00 NOTICE (5): Unexpected Error: mail() [<a href='function.mail'>function.mail</a>]:无法通过ip"连接到邮件服务器;port portip,验证您的SMTP"和smtp_port"在 php.ini 中设置或使用 ini_set() (#2).
2010-02-24 14:26:43
用户名:admin
文件 D:est.php
的第 439 行错误脚本:/customer.php
[全局错误处理程序]
-->

推荐答案

这是你能做的最好的事情:

This is about the best you can do:

if (!mail(...)) {
   // Reschedule for later try or panic appropriately!
}

http://php.net/manual/en/function.mail.php

mail() 如果邮件被成功接受发送,则返回 TRUE,否则返回 FALSE.

需要注意的是,仅仅因为邮件被接受投递,并不意味着邮件会真正到达预定目的地.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

如果需要抑制警告,可以使用:

If you need to suppress warnings, you can use:

if (!@mail(...))

使用 @ 时要小心 操作符没有适当检查某事是否成功.

Be careful though about using the @ operator without appropriate checks as to whether something succeed or not.

如果 mail() 错误不可抑制(很奇怪,但现在无法测试),您可以:

If mail() errors are not suppressible (weird, but can't test it right now), you could:

a) 暂时关闭错误:

$errLevel = error_reporting(E_ALL ^ E_NOTICE);  // suppress NOTICEs
mail(...);
error_reporting($errLevel);  // restore old error levels

b) 使用不同的邮件程序,如 火灾迈克.

b) use a different mailer, as suggested by fire and Mike.

如果 mail() 被证明过于脆弱和不灵活,我会考虑 b).关闭错误会使调试变得更加困难,而且通常是不好的.

If mail() turns out to be too flaky and inflexible, I'd look into b). Turning off errors is making debugging harder and is generally ungood.

这篇关于如何捕获由 mail() 引起的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:46