换行电子邮件未使用正则表达式

New line Email not getting using regular expression

首先,我尝试了与该主题相关的所有问题和答案。此外,我尝试了相关问题并尝试解决它们但没有成功。所以请仔细阅读我的问题。

问题:无法在 Array 上获取“Lucy 3”电子邮件。

我想要所有电子邮件商店category-wise(To / CC / BC /BCC)。我收到一封电子邮件 header 并使用正则表达式过滤器电子邮件。它工作正常。但如果 ToFrom 任何电子邮件添加一个新行,这样新行电子邮件就不会进入我的阵列。

演示 link Here

代码

$String_Header = 'From support@test.co.uk  Wed Nov 20 15:48:00 2013
Return-Path: <support@test.co.uk>
X-Original-To: mailbox@my.domain.com
Delivered-To: mailbox@my.domain.com
Delivered-To: techinfo@test.co.uk
from: "Lucy 1" <support@lucky1.co.uk>,"Lucy 2" <support@lucky2.co.uk>,
"Lucy 3" <support@lucky3.co.uk>
to: TestTo <techinfo@test.co.uk>';

function ProcessTicketPostHeaders($headers)
{
    if (is_string($headers)) {
        $_headersWeWantToKeep = array("from", "to", "cc", "bcc");
         $headers = preg_replace('/\r?\n /', ' ', $headers);        
        $_pattern = '/^(' . implode('|', $_headersWeWantToKeep) . '):.*$/im';       
        if (preg_match_all($_pattern, $headers, $headers)) {                
            $headers = implode("\n", $headers[0]);
        }
    }
 
    return $headers;
}

echo '<pre>';
print_r(ProcessTicketPostHeaders($String_Header));

输出:

from: "Lucy 1" ,"Lucy 2" ,
to: TestTo

我要输出:

from: "Lucy 1" ,"Lucy 2"  ,"Lucy 3"
to: TestTo

您可以匹配所有不以 $_headersWeWantToKeep 中的任何值开头的行,或者如果您不想匹配上次匹配之后的所有行,则可以匹配空行。

完整的模式可能看起来像

^(from|to|cc|bcc):.*(?:\R(?!(?:(?1)|$)).*)*

Regex demo | Php demo

例如

$String_Header = 'From support@test.co.uk  Wed Nov 20 15:48:00 2013
Return-Path: <support@test.co.uk>
X-Original-To: mailbox@my.domain.com
Delivered-To: mailbox@my.domain.com
Delivered-To: techinfo@test.co.uk
from: "Lucy 1" <support@lucky1.co.uk>,"Lucy 2" <support@lucky2.co.uk>,
"Lucy 3" <support@lucky3.co.uk>
to: TestTo <techinfo@test.co.uk>';

function ProcessTicketPostHeaders($headers)
{
    if (is_string($headers)) {
        $_headersWeWantToKeep = array("from", "to", "cc", "bcc");
        $headers = preg_replace('/\r?\n /', ' ', $headers);
        $_pattern = '/^(' . implode('|', $_headersWeWantToKeep) . '):.*(?:\R(?!(?:(?1)|$)).*)*/m';
        if (preg_match_all($_pattern, $headers, $headers)) {
            $headers = implode("\n", $headers[0]);
        }
    }

    return $headers;
}

echo '<pre>';
print_r(ProcessTicketPostHeaders($String_Header));

输出

<pre>from: "Lucy 1" <support@lucky1.co.uk>,"Lucy 2" <support@lucky2.co.uk>,
"Lucy 3" <support@lucky3.co.uk>
to: TestTo <techinfo@test.co.uk>