简体   繁体   English

如何知道php邮件是否失败

[英]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 . 我从php mail()发送邮件:如果发送失败到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 !! 但是,虽然$to mail不存在,但它没有显示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 检查mail的退货

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. 如果邮件成功接受传递,则返回TRUE,否则返回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 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... 虽然它返回true的事实可能意味着你的邮件程序正在接受邮件但是当它试图发送给任何人时失败...

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. 您应该通过验证器运行$to来检查它的有效地址,如果不是,则抛出错误,不要依赖mail()过滤掉你已经知道错误的东西,或者可以轻松检查。

--UPDATE --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). 然后查看@SeRPRo,但你想要做的是以编程方式进行测试的艰苦工作 - 发送电子邮件要求用户点击链接以验证它是真实的,而不是尝试查询SMTP服务器更容易和更可靠都有不同的行为(读:被打破到不同程度)。 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 !! 但是,虽然$ to mail不存在,但它没有显示失败!!

actually the fact that mail is being delivered to SMTP server, doesn't mean it will be delivered to the end user. 实际上,邮件被传递到SMTP服务器的事实并不意味着它将被传递给最终用户。 There's no easy way in PHP to check whether it's delivered. 在PHP中没有简单的方法来检查它是否已经交付。

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(). 在我的例子中,它有助于通过命令行参数“-f”设置返回路径,该参数可以在mail()的$ additional_parameters参数中传递。 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) ...根据http://www.php.net/manual/en/function.mail.php上的一些评论,hosting-inviroments反应不同,有不同的限制(地址可能需要在同一个托管帐户中注册,或者在同一个域上,与heade中的“From:”相同...等等)

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 我收到反弹的页面(看似没有上述限制)在Domainfactory http://www.df.eu上发布

Use phpmailer to send email and set $mail->AddCustomHeader('Return-path:bounce@mail.com'); 使用phpmailer发送电子邮件并设置$ 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. 如果收件人邮件ID不存在或收件人未收到任何其他案件的电子邮件,则会在bounce@mail.com发送退回邮件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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