php imap来自电子邮件地址

时间:2010-08-08 17:25:19

标签: php email imap

如何从包含imap_open的电子邮件中检索电子邮件地址?

如果发件人名称已知,如果我使用'from'参数,我会收到发件人姓名而不是电子邮件地址。

代码:http://gist.github.com/514207

7 个答案:

答案 0 :(得分:29)

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

答案 1 :(得分:4)

我也与之斗争,但以下工作:

// 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()给了我所需的所有信息。

答案 2 :(得分:3)

最糟糕的情况是,您可以使用以下内容自行解析标题:

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

$ matches将包含3个数组:

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

来自:http://www.php.net/manual/en/function.imap-fetchheader.php#82339

答案 3 :(得分: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);

答案 4 :(得分:0)

imap_fetch_overview可能是您正在寻找的内容:http://www.php.net/manual/en/function.imap-fetch-overview.php

可以在此处找到使用示例:http://davidwalsh.name/gmail-php-imap,特别是

echo $overview[0]->from;

此功能很简单,但有局限性。更详尽的版本在imap_headerinfohttp://www.php.net/manual/en/function.imap-headerinfo.php)中,可以返回所有标题数据的详细数组。

答案 5 :(得分:0)

直到我发现$ header是stdClass对象的数组之前一直有问题。以下两行有效:

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

答案 6 :(得分: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"
            );
        }