从imap标题中检索电子邮件地址

时间:2013-04-18 13:11:33

标签: php regex header preg-match imap

我正在使用php imap函数imap_header来提取完整​​标头和imap_fetch_overview来提取原始标头。这两个函数分别给我stdClass对象和数组。

我希望在进一步处理之前始终清理我的。有时候FROM或TO可以包含这样的东西, Test user <test@test.com>

目前在我的FROM中我只看到Test User并且没有电子邮件地址,直到我使用firebug。

如何从这些对象数组中获取test@test.com?

这是我从imap_fetch_overview

获得的结果
   Array
   (
     [0] => stdClass Object
     (
        [subject] => Testing
        [from] => Test User 
        [to] => testuser2@test.com
        [date] => Wed, 17 Apr 2013 18:43:46 +0530
        [message_id] => <abcdef1244.93784jgsfk@test.com>
        [size] => 3443
        [uid] => 1234
        [msgno] => 123
        [recent] => 0
        [flagged] => 0
        [answered] => 0
        [deleted] => 0
        [seen] => 0
        [draft] => 0
        [udate] => 1366204439
    )

)

“测试用户”旁边有一个隐藏的<test@test.com>。如何提取该电子邮件地址?

同样,这是我从imap_header

得到的
  stdClass Object
  (
     [date] => Wed, 17 Apr 2013 18:43:46 +0530
     [Date] => Wed, 17 Apr 2013 18:43:46 +0530
     [subject] => Test
     [Subject] => Test
     [message_id] => <abcdef1244.93784jgsfk@test.com>
     [toaddress] => testuser@test.com
     [to] => Array
     (
        [0] => stdClass Object
            (
                [mailbox] => testuser
                [host] => test.com
            )

    )

    [fromaddress] => Test User 
    [from] => Array
    (
        [0] => stdClass Object
            (
                [personal] => Test User
                [mailbox] => test
                [host] => test.com
            )

    )

    [reply_toaddress] => Test User 
    [reply_to] => Array
    (
        [0] => stdClass Object
            (
                [personal] => Test User
                [mailbox] => test
                [host] => test.com
            )

    )

    [senderaddress] => Test User
    [sender] => Array
    (
        [0] => stdClass Object
            (
                [personal] => Test User
                [mailbox] => test
                [host] => test.com
            )

    )

    [Recent] =>  
    [Unseen] => U
    [Flagged] =>  
    [Answered] =>  
    [Deleted] =>  
    [Draft] =>  
    [Msgno] =>  123
    [MailDate] => 17-Apr-2013 13:13:59 +0000
    [Size] => 3443
    [udate] => 1366204439
)

preg_match将是明显的答案,但似乎无法弄清楚如何在浏览器中看不到电子邮件地址的位上执行它,但在使用firebug检查时存在。任何帮助表示赞赏。

感谢。

3 个答案:

答案 0 :(得分:2)

您可以使用imap_rfc822_parse_adrlist()Docs更进一步:

$toAddresses = imap_rfc822_parse_adrlist('Test user <test@test.com>', 'localhost');

print_r($toAddresses);

Array
(
    [0] => stdClass Object
        (
            [mailbox] => test
            [host] => test.com
            [personal] => Test user
        )

)

答案 1 :(得分:0)

如果std对象的数组名是$ arr

$arr[0]->to

返回你想要的东西。

在第二种情况下

$ stdObject&lt; - 第二个函数返回的对象

foreach($stdObject->to as $to)
{
 echo $to->mailbox;
 echo $to->host;
}

答案 2 :(得分:0)

我刚刚发现了这个线程,虽然它很旧,但我希望此评论可能对您有所帮助。我相信我已经回答了这个查询。碰巧的是,发件人电子邮件的格式是导致此问题的原因。

From Name <from@name.com>

由于<>标记,浏览器正在将其变成HTML标记,所以我想有些类似于跨度。通过使用str_replace将“ <”更改为“”(或其他任何内容),您可以在问题到达HTML之前将其删除

相关问题