简体   繁体   中英

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 = 'itsdfdsf@7sisters.in';
    $email_from = "info@7sisters.in";

    $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 !!

The mail method is just sending the mail out. If it does not receive any errors (eg 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.

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.

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

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.

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 address.where.i.want.the.bounces@xy.com");

... 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

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

您可以将自己视为测试它离开发件箱的一种方式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM