how to know if php mail failed


how to know if php mail failed

I am sending mails from php mail() : and I want to receive a failed message if sending is failed to the destinatio .
$to = ‘’;
$email_from = “”;

$full_name = ‘XXXX’;
$from_mail = $full_name.’<'.$email_from.'>‘;

$subject = “testing sender name”;
$message = “”;
$message .= ‘

This is only a test mail. Please do not reply.
$from = $from_mail;

//$headers = “” .
// “Reply-To:” . $from . “\r\n” .
// “X-Mailer: PHP/” . phpversion();

$headers = “From:” . $from_mail . “\r\n” .
“Reply-To:” . $from_mail . “\r\n” .
“X-Mailer: PHP/” . phpversion();

$headers .= ‘MIME-Version: 1.0’ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;

echo ‘failed !!’;

But although $to mail does no exist,it is not showing failed !!


Solution 1:

I think what you want is to check for a real email not only a valid formatted email. So I would suggest you to have a look at this blog

Solution 2:

The mail method is just sending the mail out. If it does not receive any errors (e.g. by not finding the server etc), it will return succesfull. You will not be able to know if the mail actually landed in the inbox of the recipient unless you create some code around bounced emails etc.

Solution 3:

check the return from of mail

Returns TRUE if the mail was successfully accepted for delivery, FALSE

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

Although the fact it is returning true probably means that your mail program is accepting the message but then failing when it tries to send to no one…

You should run the $to through a validator to check its a valid address and then throw an error if its not, don’t rely on mail() to filter out things which you already know are wrong, or can check against easily.


Then check out @SeRPRo , but what your trying to do is hard work to test programatically – its far easier and more reliable to send an e-mail which requires the user to click a link to verify that it’s real than try querying SMTP servers which all have different behaviour (read: are broken to different degrees). Also note that your intended behaviour (code wise) is hard to differentiate from a spammers so don’t be surprised to find it difficult going if you avoid the verification e-mail route.

Solution 4:

But although $to mail does no exist,it is not showing failed !!

actually the fact that mail is being delivered to SMTP server, doesn’t mean it will be delivered to the end user. There’s no easy way in PHP to check whether it’s delivered.

Solution 5:

You could CC yourself as a way of testing that it is leaving the outbox.

Solution 6:

In my case it helped to set the return-path via the commandline parameter “-f”, which can be passed in the $additional_parameters parameter of mail(). so i call

mail($to, $subject, $message, $headers, "-f");

… according to some comments on hosting-inviroments react different and have different restrictions (address might need to be registered in the same hosting-account, or be on the same domain, the same as the “From:” in the heade … and so on)

The page where I got the bounces to be received (with non of the mentioned restrictions, as it seems) is hosted at Domainfactory

Solution 7:

Use phpmailer to send email and set $mail->AddCustomHeader(‘’);
This will send bounce email at if recipient mail id does not exist or recipient does not receive email by any other case.