How to Get the Error Message For the Mail() Function

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.

PHP Mail Not Sending But No Errors

Try this to find out where is the problem

$success = mail('example@example.com','New Enquiry',$msg);
if (!$success) {
print_r(error_get_last()['message']);
}

with a glance at Php Mail Documentation

Note:
When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.

Return Values


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.

update
After comments just for test, remove every code in your php file an just try the simplest way to debug

<?php
// the message
$msg = "First line of text\nSecond line of text";

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
if(!mail("someone@example.com","My subject",$msg)){
var_dump(error_get_last()['message']);
}
?>

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.



Related Topics



Leave a reply



Submit