如果
电子邮件在通过PHP函数mail()发送电子邮件时被退回,我想保存电子邮件?抓住它的最佳方法是什么?
首先,您需要使用其他标头发送带有mail()函数的电子邮件。将返回电子邮件地址指向将收集退回电子邮件的邮箱。下面的代码是如何实现这一点的示例。
$recipient = "jack@someotherexample.com";
$subject = "test subject";
$message = "test message";
$body = "<html>rn";
$body .= "<body style="font-family:Verdana, Verdana, Geneva, sans-serif; font-size:12px; color:#666666;">rn";
$body .= $message;
$body .= "</body>rn";
$body .= "</html>rn";
$headers = "From: My site<noreply@example.com>rn";
$headers .= "Reply-To: bounce@example.comrn";
$headers .= "Return-Path: bounce@example.comrn";
$headers .= "X-Mailer: PHPrn";
$headers .= 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
$result = mail($recipient, $subject, $body, $headers);
if($result)
{
echo "email sent";
}
else
{
echo "email send error";
}
源
而不是您只是检查收件箱中是否有退回的错误电子邮件。下面的示例代码将连接到 imap 收件箱,获取并打印出它在那里找到的所有电子邮件。您可以根据需要轻松自定义它。
/* connect to server */
$hostname = '{example.com:143}INBOX';
$username = 'bounce@example.com';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to mailbox: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,1.2);
/* output the email header information */
$output.= '<div '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span>'.$overview[0]->subject.'</span> ';
$output.= '<span>'.$overview[0]->from.'</span>';
$output.= '<span>'.$overview[0]->date.'</span>';
$output.= '</div>';
/* output the email body */
$output.= '<div class="body">'.$message.'</div>';
}
echo $output;
}
/* close the connection */
imap_close($inbox);
源
将退回
电子邮件地址添加到电子邮件标头,并监控来自 PHP 的电子邮件帐户是否有退回邮件。