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

时间:2021-08-13 07:13:20

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:

邮件服务器宕机导致的错误如下:

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

< !(5):意外错误:mail() [函数。邮件]:未能在“ip”端口端口端口端口端口端口端口连接到mailserver,请在php中验证您的“SMTP”和“smtp_port”设置。或者使用ini_set() (# 2). 2010-02-24 14:26:43 Username: admin Error in line 439 of file D:\test。php脚本:/客户。php[全局错误处理程序]——>。

5 个解决方案

#1


43  

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 TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

如果邮件成功地接受交付,则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.

在使用@操作符时要小心,不要对某些操作是否成功进行适当的检查。


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 fire and Mike.

b)根据fire和Mike的建议,使用不同的邮件。

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).关闭错误会使调试更加困难,并且通常是不好的。

#2


6  

PHPMailer handles 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!";
}

#3


4  

also using http://php.net/error_get_last will 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.

唯一的方法似乎是使用一个合适的邮件,就像上面已经提到的。

#4


3  

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]元素来显示错误。

#5


1  

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

您可以使用PEAR邮件类和方法,通过以下方式检查错误:

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

You can find an example here.

你可以在这里找到一个例子。

#1


43  

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 TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

如果邮件成功地接受交付,则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.

在使用@操作符时要小心,不要对某些操作是否成功进行适当的检查。


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 fire and Mike.

b)根据fire和Mike的建议,使用不同的邮件。

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).关闭错误会使调试更加困难,并且通常是不好的。

#2


6  

PHPMailer handles 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!";
}

#3


4  

also using http://php.net/error_get_last will 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.

唯一的方法似乎是使用一个合适的邮件,就像上面已经提到的。

#4


3  

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]元素来显示错误。

#5


1  

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

您可以使用PEAR邮件类和方法,通过以下方式检查错误:

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

You can find an example here.

你可以在这里找到一个例子。