php 如何捕获由 mail() 引起的错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2323463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:02:12  来源:igfitidea点击:

How can I catch an error caused by mail()?

phpemail

提问by Jin Yong

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?

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

Error that was caused by emailserver down is below:

由 emailserver down 引起的错误如下:

<!--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]
-->

<!--2010-02-24T14:26:43+11:00 通知 (5):意外错误:mail() [<a href='function.mail'>function.mail</a>]:未能连接到邮件服务器的“ip”端口 portip,验证 php.ini 中的“SMTP”和“smtp_port”设置或使用 ini_set() (#2)。
2010-02-24 14:26:43
用户名:admin
文件 D:\test.php 的第 439 行错误
脚本:/customer.php
[全局错误处理程序]
-->

回答by deceze

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

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

mail()returns TRUEif the mail was successfully accepted for delivery, FALSEotherwise.

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.

mail()TRUE如果邮件被成功接受交付,FALSE则返回,否则返回。

重要的是要注意,仅仅因为邮件被接受交付,并不意味着邮件实际上会到达预定的目的地。

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.

但是在@没有适当检查某事是否成功的情况下使用运算符时要小心。



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

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

a) turn off errors temporarily:

a) 暂时关闭错误:

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

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

b) 按照fireMike 的建议,使用不同的邮件程序。

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()结果证明太片状和不灵活,我会考虑 b)。关闭错误会使调试变得更加困难,而且通常是不好的。

回答by fire

PHPMailerhandles errors nicely, also a good script to use for sending mail via SMTP...

PHPMailer 可以很好地处理错误,也是用于通过 SMTP 发送邮件的好脚本...

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

回答by staabm

also using http://php.net/error_get_lastwill not help you out, because mail() does not emmit its errors into this function.

同样使用http://php.net/error_get_last也不会帮助你,因为 mail() 不会将它的错误发送到这个函数中。

Only way seems to be using a proper mailer, like already suggested above.

唯一的方法似乎是使用适当的邮件程序,就像上面已经建议的那样。

回答by Dairy Window

According to http://php.net/manual/en/function.error-get-last.php, use:

根据http://php.net/manual/en/function.error-get-last.php,使用:

print_r(error_get_last());

Which will return an array of the last error generated. You can access the [message]element to display the error.

这将返回生成的最后一个错误的数组。您可以访问该[message]元素以显示错误。

回答by Mike Trpcic

You could use the PEAR Mailclasses and methods, which allows you to check for errors via:

您可以使用PEAR Mail类和方法,它允许您通过以下方式检查错误:

if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
} else {
    echo("<p>Message successfully sent!</p>");
}

You can find an example here.

您可以在此处找到示例。