简体   繁体   中英

How to get failed response from SMTP mail server?

I am working on Laravel and I am trying to send an email with PHPMailer and my mail server is Zoho .

Here is my Code:

$mail = new PHPMailer(true);

$mail->isSMTP();
$mail->Host = 'smtp.zoho.com';
$mail->SMTPAuth = true;
$mail->Username = 'ServerUserName';
$mail->Password = 'ServerUserPassword';
$mail->SMTPSecure = 'TLS';
$mail->Port = 587;
$mail->setFrom('itsupport@foo.net', 'Foo');
$mail->addAddress($email);
$mail->addReplyTo('noreply@foo.com', 'No-reply');
$mail->isHTML(true);
$mail->Subject = "Testing - " . $subject;
$mail->Body = $body;

if(!empty($attachment)) {
    $mail->AddAttachment($attachment['abs_path'], $attachment['name']);
}

$mail->SMTPDebug = 2;
$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str"; echo "<br>";};

if(!$mail->Send()) {
    $error = 'Mail error: '.$mail->ErrorInfo;
    echo "Error"."<br>"."================================="."<br>"."<br>";
    dd($mail);
} else {
    $error = 'Message sent!'.$mail->ErrorInfo;
    echo "Success"."<br>"."================================="."<br>"."<br>";
    dd($mail);
}

My Scenarios

1) When I am sending an email with my Valid and Real email it gives me success status and in my mail server its shows me in Sent tab.

在此处输入图像描述

2) But when I am sending an email with Valid but Fake email it gives me also Success status and when I check my mail server It gives me this mail.

在此处输入图像描述

3) And in both scenarios in my debugging code it gives me always this return:

在此处输入图像描述

What I want I want when a mail server return undelivered or failed mail it should return me an error code eg (550, 552, 553 etc) + Error Message. I search a lot but not find anything.

Is there any possibility that server return me the error code also.

Note: I tried my phpmailer code in try catch. But when I use fake email it dos not goes to catch.

You're misunderstanding the structure of email. Email uses an asynchronous store-and-forward approach, which means that can be sent successfully, but fail later, before it reaches its destination. This is completely unlike HTTP which gives immediate responses.

You are submitting the message successfully to Zoho's mail server, but then that server is failing to deliver the message to its intended destination, so it gets sent back as a bounce to the envelope sender address (the address in the SMTP MAIL FROM command, set via the Sender property in PHPMailer).

To handle bounces in your code, you can configure your mail server to pipe the inbound message into a script attached to your bounce address, for example as described in this article .

Unfortunately that's not the end of the story. While you will then have programmatic access to the bounced message, actually figuring out why it bounced and who bounced it is often difficult, if not impossible in some cases. For example, Microsoft Exchange sometimes sends bounces that do not contain any means of identifying the address the original message was sent to! You can address that particular shortcoming by using VERP addressing , where every message has a unique bounce address, which you can do with PHPMailer.

Writing bounce handlers is generally a very unpleasant experience that I recommend avoiding if you can. There is a commercial bounce classification library by BoogieTools that works well, but there is still a large element of guesswork and heuristics involved.

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