使用 PHP 处理邮件失败?

发布于 2024-10-12 12:35:27 字数 1723 浏览 5 评论 0原文

如果我通过 GMail 向此地址发送电子邮件 [电子邮件]受保护]

我收到如下错误:

向以下收件人的递送永久失败

我的问题是,如果我使用 PHP mail 功能发送,如何捕获退回邮件?

我的代码:

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

$to = "[email protected]";
$subject = "Testing";
$message = "Testing body";

mail($to, $subject, $message, $headers); 

If I send an email through GMail to this address [email protected]

I got error like:

Delivery to the following recipient failed permanently

My question is, if I send using the PHP mail function, how can I catch bounce emails?

My code:

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

$to = "[email protected]";
$subject = "Testing";
$message = "Testing body";

mail($to, $subject, $message, $headers); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

往日情怀 2024-10-19 12:35:27

仅使用 mail() 无法检测到此类错误。您必须指定一个有效的返回地址,然后连接到该邮箱(通过 IMAP 或 POP,有相关功能)并检查是否有邮件被退回。请记住,退回可能需要很长时间(由于暂时性错误,甚至可能需要几个小时),因此您必须异步执行此检查。

You can't detect such errors just by using mail(). You have to specify a valid return address and then connect to that mailbox (via IMAP or POP, there are functions for that) and check if any message bounced. Bear in mind that bounces may take a long time (even hours, due to transient errors) so you have to do this check asynchronously.

仅冇旳回忆 2024-10-19 12:35:27

使用此功能时,您可以通过邮件收到消息:

$bounce = '[email protected]';
mail($to, $subject, $message, $headers,"-f $bounce");

这会将所有退回邮件发送到您的地址

you could get the message by mail when you use this:

$bounce = '[email protected]';
mail($to, $subject, $message, $headers,"-f $bounce");

This will send all bounce back mails to your address

滿滿的愛 2024-10-19 12:35:27

您可以使用 PEAR MAil,它包含一个 parseAddressList () 方法,检查电子邮件地址的服务器是否接受该电子邮件地址。它还返回有用的错误消息。

You could use PEAR MAil, it contains a parseAddressList() method which checks if the server of an email address accepts the email address. It also returns useful error messages.

半仙 2024-10-19 12:35:27

PHP mail() 函数确实有一个错误状态作为函数返回值,但它不会检测到这种错误,因为它只检查电子邮件是否已发送,而不是已发送。已收到。

(这是因为它需要立即将控制权返回给程序;它不能等待可能的退回消息,因为它们可能需要很长时间才能返回 - 您不希望您的程序等待五天以防万一电子邮件被退回,你会吗?)

因此,确定邮件是否被退回的唯一方法是使用一个单独的程序来检查退回邮件将发送到的邮箱。

您可以使用邮件选项字符串中的 -f 选项指定退回邮件的邮箱(请参阅 PHP mail() 函数手册页 了解更多信息)。

然后让一个单独的程序定期检查该邮箱中是否有退回邮件,并根据需要向您报告。 (有许多 PHP 库允许您检查邮箱;Google 将在这里提供帮助,或者查看 PEAR)

确定是哪封电子邮件将取决于退回邮件的质量。您绝对应该能够看到预期收件人的电子邮件地址,但您可能无法分辨出它是哪封电子邮件,即将其与您调用 mail() 时的特定实例进行匹配功能放在第一位。

希望有帮助。

The PHP mail() function does have an error status as the function return value, but it won't pick up this kind of error because it only checks that the email has been sent, not that it has been received.

(this is because it needs to return control back to the program immediately; it can't wait for a possible bounce message because they can take a long time to come back - you wouldn't want your program to sit and wait for five days just in case the email bounced, would you?)

Therefore, the only way to determine whether a message has bounced is to have a separate program which checks the mailbox which the bounces would be sent to.

You can specify the mailbox for bounces using the -f option in the mail options string (see the PHP mail() function man page for more info on this).

Then have a separate program periodically check that mailbox for bounce messages, and report them to you as required. (there are a number of PHP libraries that allow you to check a mailbox; Google will help here, or look in PEAR)

Determining which email it was will depend on the quality of the bounce message. You should definitely be able to see the intended recipient's email address, but you may not be able to tell which email it was, ie to match it up to a specific instance of when you called the mail() function in the first place.

Hope that helps.

乱世争霸 2024-10-19 12:35:27

您应该通过 IMAP 或 POP3 登录到您的收件箱并解析电子邮件地址和传送错误。

还值得一提的是,您可以指定(在发送时)自定义标头以更好地跟踪消息源,我相信 MailChimp 和其他人这样做会跟踪活动等......

You should login via IMAP or POP3 to your inbox and parse the email address and delivery error.

It's also worth mentioning that you can specify (while sending) custom headers to better track the message source, I believe MailChimp and others do this do track campaigns, etc...

故事还在继续 2024-10-19 12:35:27

PHP 在退出邮件功能后对邮件一无所知,因此无法在发送邮件的同时执行此操作。

PHP does not know anything about the mail after it got out of the mail function, so no way to do it at the same time as you send the mail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文