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>]: 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_mail = mail('[email protected]', 'My Subject', $message); if($this_mail) echo 'sent!'; else echo error_message; html. css.
Syntax. mail(to,subject,message,headers,parameters);
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()
returnsTRUE
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.
If mail()
errors are not suppressible (weird, but can't test it right now), you could:
a) turn off errors temporarily:
$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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With