How to Catch an Error Caused by Mail()

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

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.

Catching PHP mail() errors and showing reasonable user error message

Use the boolean result to detect an error:

$success = @mail(...);

Then you want to find out which internal error caused the problem, so use:

$error = error_get_last();
preg_match("/\d+/", $error["message"], $error);
switch ($error[0]) {
case 554:
...
default:
...

Note that this works with php 5.2 onward only.

There is no way to verify delivery or see transport error mails with PHP. You would need a pop3 polling handler for that.

Get full error message from PHPMailer Exception

After setting $mail->SMTPDebug = 2;, you need to create a callback function and assign it to $mail->Debugoutput. In that function, you can assign the debug output to a variable. Here's the documentation on this subject: http://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#property_Debugoutput

Please note: Your callback function will be called once per line of debug output (as opposed to just once with all of the debug output in the error string), so you'll have to append each line to your variable. If you just assign it, you'll only get the last line of debug output, which is often the same information that's in $mail->ErrorInfo or the exception.

I prefer to do something like this:

$GLOBALS['debugOutput'] = [];

$mail->Debugoutput = function($debugOutputLine, $level) {
$GLOBALS['debugOutput'][] = $debugOutputLine;
};

//...Put your mail code here that could cause an error

$debug_output = implode("\n", $GLOBALS['debugOutput']);
echo $debugOutput;

This should print out the same information as in your example.

Java Mail: Exception when sending email

it turns out I was running into multiple issues:

  1. Issue with tutorial

It uses com.sun.mail.smtp.SMTPMessage but in my case that doesn't work but using javax.mail.internet.MimeMessage works fine.


  1. Root cause of error

Above code runs within a 3rd party eclipse based application and they seem to interfere with each other. The solution for this can be found here:

Thread t =  Thread.currentThread();
ClassLoader ccl = t.getContextClassLoader();
t.setContextClassLoader(session.getClass().getClassLoader());
try {
Transport.send(m);
} finally {
t.setContextClassLoader(ccl);
}

Adjusting the code accordingly make it work.

PHP's mail() function causes a 500 Internal Server Error only after a certain point in the code

Transferring to a different server with similar configuration solved the problem, so it appears that it was, in fact, and issue with the host's server (specific cause is unknown, and regrettably, it will probably remain a mystery).

Mail Sender Exception (Using mailtrap.io)

I managed to fix the error.

Problem was within 'sendVerificationEmail' method.

Instead of using:

message.setReplyTo(emailAddress);

I needed to use:

message.setTo(emailAddress);


Related Topics



Leave a reply



Submit