使用正则表达式获取失败的传递电子邮件

时间:2016-07-03 11:20:40

标签: php regex

我正在解析失败的传递电子邮件,然后将其记录到数据库以便再次发送,但它在preg_match部分失败了。我确定该模式是正确的,在regex101.com上多次检查,但功能的结果总是错误的。

$inbox = imap_open('{imap.gmail.com:993/imap/ssl}INBOX', $username, $pass) or die('Cannot connect to gmail: ' . imap_last_error());

$emails = imap_search($inbox, 'SINCE ' . $since);
$invalidEmails = array();
if ($emails) {
    rsort($emails);
    foreach ($emails as $email) {
        $message = imap_fetchbody($inbox, $email, 1);
        if(preg_match("/^Delivery to the following recipient failed permanently:\n\n\s+(.+)/im", $message, $matches)) {
            Debugger::log("mail found");
        }

        array_push($invalidEmails, $matches);

        if(preg_match("/^Delivery to the following recipient has been delayed:\n\n\s+(.+)/im", $message, $matches)) {
            Debugger::log("mail found");
        }

        array_push($invalidEmails, $matches);
    }
}

imap_close($inbox);

$ message的内容:

Delivery to the following recipient failed permanently:

     123@blabla.com

Technical details of permanent failure:=20
Google tried to deliver your message, but it was rejected by the server for=

我已经被这几个小时困住了,有人知道会出现什么问题吗?

1 个答案:

答案 0 :(得分:0)

替换

preg_match("/^Delivery to the following recipient failed permanently:\n\n\s+(.+)/im", $message, $matches)

通过

preg_match("/^Delivery to the following recipient failed permanently:\s+(.+)/im", $message, $matches)