Search code examples
phperror-handlingsendmailerror-reporting

PHP error reporting and mail()


I've currently got set_error_handler() throwing ErrorException whenever anything is encountered. Furthermore, I've got error_reporting() at -1, so mis-indexing an array throws an exception.

Anyways, that being a cursory overview of my error/reporting environment; the PHP mail() function is tossing errors on my (Win7) dev machine, during a process intended for a project, 'cause I don't have a mail server installed. I tried prefixing it with @ to shut it up, but it still triggers an error, and hence throws an exception. I could wrap it with try{}, but I'm curious as to why it won't shut up.

Why won't it shut up?

To reiterate, we can almost remove mail() from the equation (so far as I can tell); I just want to know why @ wouldn't do it's job under such a circumstance, with mail() or any function. I just figured mail() may be special case for some reason.


Thanks to our friend XDebug:

( ! ) ErrorException: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\dbc_relaunch_2-0-0\mf\handlers\api\mail\send.php on line 16


Alrighty, I've simply gone with:

try{
    mail($args);
catch(Exception $exception){
    return $failure;
}
return $success;

Rather than:

if(@mail($args)){
    return $failure;
}
return $success;

And all is well; I'm browsing the answer @Pheonix linked, but anyone with a short order response as to why this is free to answer.


Solution

  • Could you show us your error handler? Note that your handler will be called in case of an error, regardless of your error_reporting() setting and even if you prepend your function with an @.

    Of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator.

    From the manual page of set_error_handler().

    If you don't want to throw exceptions for errors caused by an @ prefixed expression, you'll have to check the return value of error_reporting() in your handler. In case it returns zero, you're dealing with a suppressed error.