Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

email

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

like image 279
Jin Yong Avatar asked Feb 24 '10 03:02

Jin Yong


People also ask

How can I get the error message for the mail function?

$this_mail = mail('[email protected]', 'My Subject', $message); if($this_mail) echo 'sent!'; else echo error_message; html. css.

What is the syntax of mail () function?

Syntax. mail(to,subject,message,headers,parameters);


1 Answers

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.

like image 67
deceze Avatar answered Sep 23 '22 20:09

deceze