如何从包含imap_open的电子邮件中检索电子邮件地址?php imap从电子邮件地址获取
如果发件人名称已知,如果使用'from'参数,则获取发件人名称而不是电子邮件地址。
代码:http://gist.github.com/514207
如何从包含imap_open的电子邮件中检索电子邮件地址?php imap从电子邮件地址获取
如果发件人名称已知,如果使用'from'参数,则获取发件人名称而不是电子邮件地址。
代码:http://gist.github.com/514207
最坏的情况,你可以分析自己的头的东西,如:
<?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])
从得到这个
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_headerinfo
(http://www.php.net/manual/en/function.imap-headerinfo.php),它可以返回所有标题数据的详细数组。
我争夺这个很好,但以下几项工作:
// 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()给我我需要的所有信息。
有同样的问题,你......必须拼凑在一起,不知道为什么它是这样的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);
有麻烦,直到我发现了$头是stdClass的对象的阵列。下面两行的工作:
$header=imap_fetch_overview($imap,$countClients,FT_UID);
$strAddress_Sender=$header[0]->from;
完整的工作代码与在线例子
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"
);
}
尽管这个链接可能回答这个问题,但最好在这里包含答案的基本部分,并提供参考链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/18526256) – Eineki 2018-01-16 02:02:49
完成。感谢评论! – 2018-01-16 02:16:09
这是我的头: 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