如何捕获由mail()引起的错误? [英] How can I catch an error caused by mail()?

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

问题描述

有没有人知道如何捕获邮件错误(错误显示,同时发现电子邮件,错误是由邮件服务器导致的)php?

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

电子邮件服务器如下:


<! - 2010-02-24T14:26:43 + 11:00注意(5):意外的错误:mail()[< a href ='function.mail'> function.mail< / a>]:无法连接到mailserver在ip端口端口,验证您的SMTP和smtp_port设置在php.ini或使用ini_set()(#2)。

2010-02-24 14:26:43

用户名:admin

错误在文件D的第439行:\test.php

脚本:/customer.php

[全局错误处理程序]

- >

<!--2010-02-24T14:26:43+11:00 NOTICE (5): Unexpected Error: mail() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at "ip " port portip, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() (# 2).
2010-02-24 14:26:43
Username: admin
Error in line 439 of file D:\test.php
Script: /customer.php
[Global Error Handler]
-->


推荐答案

这是关于你最好的做法:

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 否则

mail() returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

重要的是要注意,只是因为邮件被接受送达,这并不意味着邮件将实际到达目的地。

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)使用不同的邮件程序,如 fire Mike

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()引起的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆