简体   繁体   English

如何在imap php中提取完整的邮件地址

[英]how to extract full mail address in imap php

i use the below code to extract header details from the mail.. i could not get the mail address in from, to and cc as mentioned below.. 我使用下面的代码从邮件中提取标题详细信息..我无法在下面提到的from,to和cc中获取邮件地址..

$header = explode("\n", imap_fetchheader($mbox,$msgno));
echo "<br>";
    for ($i=1; $i<count($header); $i++)
    {
        echo $header[$i] . "<br>";
    }


output:

Delivered-To: user1@examplecom 
X-WM-Delivered: user1@example.com 
Received: from ElcotPC ([127.0.0.1]) 
(envelope-sender ) 
by 127.0.0.1 with ESMTP 
for ; Wed, 31 Jul 2013 09:14:19 +0530 
From: "user1" 
To: 
Cc: 
Subject: testing with attachment 
Date: Wed, 31 Jul 2013 09:14:18 +0530 

The "from","to", "cc" field are empty without the mail address.. 没有邮件地址,“from”,“to”,“cc”字段为空。

i want the output like this.. 我想要像这样的输出..

 Delivered-To: user1@examplecom 
    X-WM-Delivered: user1@example.com 
    Received: from ElcotPC ([127.0.0.1]) 
    (envelope-sender ) 
    by 127.0.0.1 with ESMTP 
    for ; Wed, 31 Jul 2013 09:14:19 +0530 
    From: "user1" <user1@example.com>
    To: <user2@example.com>
    Cc: <user1@example.com>

how to get the email address to "from", "to" and "cc" field? 如何将电子邮件地址设置为“从”,“到”和“cc”字段?

Update: 更新:
It's always best to use code that is readily available, so I checked if a imap-parsing function exists already. 最好使用现成的代码,所以我检查了是否已存在imap-parsing函数。 It does: imap_rfc822_parse_headers . 它确实: imap_rfc822_parse_headers Read the docs for details, and links to all sorts of imap_* functions. 阅读文档了解详细信息,并链接到各种imap_*函数。 Perhaps imap_rfc822_parse_adrlist is exactly what you need? 也许imap_rfc822_parse_adrlist 正是你需要的?


A basic preg_match_all call could do the job, I think: 我认为基本的preg_match_all调用可以完成这项工作:

if (preg_match_all('/^\s*(From|To|Cc):[^<]*<([^>]+)\>/m',$string, $addresses)
{
    $addresses = array_merge($addresses[1], $addresses[2]);
    print_r($addresses);
}

Should output: 应输出:

array (
    'From' => 'user1@example.com',
    'To' => 'user2@example.com',
    'Cc' => 'user1@example.com',
)

I think that's what you were looking for. 我认为这就是你要找的东西。
The regex explained: 正则表达式解释说:

  • ^\\s* matches the start of the line, and zero or more whitespace chars ^\\s*匹配行的开头,以及零个或多个空格字符
  • (From|To|Cc) matches (and groups) From, To or Cc (From|To|Cc)匹配(和组)From,To或Cc
  • :[^<]*< : Matches (but doesn't group) the colon, and any char, except for the address delimiting < :[^<]*< :匹配(但不分组)冒号和任何字符,但地址分隔<
  • ([^>]+) : Mathces (and groups) everything after the < , that isn't > ([^>]+) :在< ,not >之后的所有内容的Mathces(和分组)
  • \\> : Can be left out, but matches address-delimiting > \\> :可以省略,但匹配地址分隔>
  • m : multi-line. m :多线。 If left out the leading ^ means start of string , now it means start of line 如果省略前导^意味着字符串的开始 ,现在它意味着开始行

Notes: This expression doesn't deal with comma separated addresses or multiple addresses, and it might be usefull to call: 注意:此表达式不处理逗号分隔的地址或多个地址,并且调用它可能很有用:

 filter_var($addresses['From'], FILTER_VALIDATE_EMAIL)

or use array_map to filter $addresses[2] prior to merging... 或者在合并之前使用array_map过滤$addresses[2] ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM