2010-08-08 144 views

回答

3

最坏的情况,你可以分析自己的头的东西,如:

<?php 
$headers=imap_fetchheader($imap, $msgid); 
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $headers, $matches); 
?> 

$比赛将包含3列:http://www.php.net/manual/en/function.imap-fetchheader.php#82339

$matches[0] are the full-lines (such as "To: [email protected]\r\n") 
$matches[1] will be the header (such as "To") 
$matches[2] will be the value ([email protected]) 

从得到这个

+0

这是我的头: MIME-版本:1.0 收稿日期:由10.227.37。212; 2010年7月21日星期三12:21:40 -0700(PDT) 日期:2010年7月21日星期三12:21:40 -0700 消息ID: 主题:使用颜色和主题自定义Gmail 来自:Gmail团队 发送给:Frederik Heyninck Content-Type:multipart/alternative; boundary = 0016e6d5fc53164fb6048beab667 所以没有电子邮件地址。那就是问题所在。 – 2010-08-09 21:43:09

3

我争夺这个很好,但以下几项工作:

// Get email address 
$header = imap_header($imap, $result); // get first mails header 
echo '<p>Name: ' . $header->fromaddress . '<p>'; 
echo '<p>Email: ' . $header->senderaddress . '<p>'; 

我用了imap_fetch_overview(),但imap_header()给我我需要的所有信息。

29
$header = imap_headerinfo($imap_conn, $msgnum); 
$fromaddr = $header->from[0]->mailbox . "@" . $header->from[0]->host; 
+0

这将如何使用Zend_Mail来实现? – beingalex 2012-06-27 17:29:25

+0

是不是Zend_Mail用于撰写和*发送*电子邮件?这个问题是关于从收到的消息中提取地址,在这种情况下通过imap访问。 – dlo 2012-07-11 19:16:31

1

有同样的问题,你......必须拼凑在一起,不知道为什么它是这样的gonzoware。

未测试这里例如:

$mbox = imap_open(....) 

$MN=$MC->Nmsgs; 
$overview=imap_fetch_overview($mbox,"1:$MN",0); 
$size=sizeof($overview); 
for($i=$size-1;$i>=0;$i--){ 
    $val=$overview[$i]; 
    $msg=$val->msgno; 
    $header = imap_headerinfo ($mbox, $msg); 
    echo '<p>Name/Email Address: ' . $header->from[0]->personal ." ". 
    $header->from[0]->mailbox ."@". $header->from[0]->host. '<p></br>'; 
} 
imap_close($mbox); 
0

有麻烦,直到我发现了$头是stdClass的对象的阵列。下面两行的工作:

$header=imap_fetch_overview($imap,$countClients,FT_UID); 
    $strAddress_Sender=$header[0]->from; 
0

完整的工作代码与在线例子

Extract email addresses list from inbox using PHP and IMAP 收件箱,使用的PHP和-IMAP

我认为,所有你需要的仅仅是复制的脚本。

我发布的代码的两个核心功能,在这里也(感谢Eineki的评论)

  function getAddressText(&$emailList, &$nameList, $addressObject) { 
      $emailList = ''; 
      $nameList = ''; 
      foreach ($addressObject as $object) { 
       $emailList .= ';'; 
       if (isset($object->personal)) { 
        $emailList .= $object->personal; 
       } 
       $nameList .= ';'; 
       if (isset($object->mailbox) && isset($object->host)) { 
        $nameList .= $object->mailbox . "@" . $object->host; 
       }  
      }  
      $emailList = ltrim($emailList, ';'); 
      $nameList = ltrim($nameList, ';'); 
     } 

     function processMessage($mbox, $messageNumber) { 
      echo $messageNumber; 
      // get imap_fetch header and put single lines into array 
      $header = imap_rfc822_parse_headers(imap_fetchheader($mbox, $messageNumber)); 
      $fromEmailList = ''; 
      $fromNameList = ''; 
      if (isset($header->from)) { 
       getAddressText($fromEmailList, $fromNameList, $header->from); 
      } 
      $toEmailList = ''; 
      $toNameList = ''; 
      if (isset($header->to)) { 
       getAddressText($toEmailList, $toNameList, $header->to); 
      }  
      $body = imap_fetchbody($mbox, $messageNumber, 1); 
      $bodyEmailList = implode(';', extractEmail($body)); 
      print_r(
       ',' . $fromEmailList . ',' . $fromNameList 
       . ',' . $toEmailList . ',' . $toNameList 
       . ',' . $bodyEmailList . "\n" 
      ); 
     } 
+0

尽管这个链接可能回答这个问题,但最好在这里包含答案的基本部分,并提供参考链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/18526256) – Eineki 2018-01-16 02:02:49

+0

完成。感谢评论! – 2018-01-16 02:16:09