tags:

views:

218

answers:

3

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?

Error that caused by emailserver down as 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]
-->

+2  A: 

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() 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.


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.

deceze
+1 Good answer. Often times people forget that `mail()` returns a `bool`. Good that you referenced the "if no error, the mail arrived" assumption too as this is a common misconception amongst new developers too.
Jonathan Sampson
Hi deceze, I understand that mail() will return true or false. But unknown reason, this is always showing the error message on my page ( which the error message show on my question above and the error only able to view when I view the source code from the page):and this error caused me unable to redirect (header() )my page to the new page. I have search around the code but unable to find out where actually display this error. Perhaps this is set in the php stmp setting?
Jin Yong
My question is ... how can I ignored or throw this "Failed to connect to mailserver" error message from my page heading? Do anyone have idea how to do it? I been searching around in google but still unable to find out the solution
Jin Yong
@Jin Yong Did the `@` operator not work in this case?
deceze
@deceze the @ is not working in this case at all..
Jin Yong
@Jin Yong See updated answer.
deceze
A: 

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

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

You can find an example here.

Mike Trpcic
A: 

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

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
fire
Actually I think it echoes an error if you didn't set the exception property to true
AntonioCS