AssamGuy
AssamGuy

Reputation: 1593

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 protected]';
    $email_from = "[email protected]";

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



    $subject = "testing sender name";
    $message = "";
    $message .= '
            <p><strong>This is only a test mail. Please do not reply.</strong><br />
    ';
    $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";        

    if(!mail($to,$subject,$message,$headers))
    {
        echo 'failed !!';
    }

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

Upvotes: 5

Views: 8266

Answers (7)

user2233109
user2233109

Reputation:

Use phpmailer to send email and set $mail->AddCustomHeader('Return-path:[email protected]'); This will send bounce email at [email protected] if recipient mail id does not exist or recipient does not receive email by any other case.

Upvotes: 0

johjoh
johjoh

Reputation: 465

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 [email protected]");

... according to some comments on http://www.php.net/manual/en/function.mail.php 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 http://www.df.eu

Upvotes: 0

SERPRO
SERPRO

Reputation: 10067

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

Upvotes: 3

user336242
user336242

Reputation:

check the return from of 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.

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.

--UPDATE

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.

Upvotes: 3

abcde123483
abcde123483

Reputation: 3905

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

Upvotes: 0

Bas Slagter
Bas Slagter

Reputation: 9929

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.

Upvotes: 5

Martin.
Martin.

Reputation: 10539

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.

Upvotes: 1

Related Questions