php imap 从电子邮件地址获取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3435293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php imap get from email address
提问by Frederik Heyninck
How do I retrieve the email address from an email with imap_open?
如何使用 imap_open 从电子邮件中检索电子邮件地址?
If the sender name is known I get the sender name instead of the email address if I use the 'from' parameter.
如果知道发件人姓名,如果我使用 'from' 参数,我会得到发件人姓名而不是电子邮件地址。
回答by dlo
$header = imap_headerinfo($imap_conn, $msgnum);
$fromaddr = $header->from[0]->mailbox . "@" . $header->from[0]->host;
回答by andrebruton
I battled with this as well but the following works:
我也与此作斗争,但以下有效:
// Get email address
$header = imap_header($imap, $result); // get first mails header
echo '<p>Name: ' . $header->fromaddress . '<p>';
echo '<p>Email: ' . $header->senderaddress . '<p>';
I had used imap_fetch_overview() but the imap_header() gave me all the information I needed.
我曾经使用过 imap_fetch_overview() 但 imap_header() 给了我我需要的所有信息。
回答by Matt Williamson
Worst case, you can parse the headers yourself with something like:
最坏的情况是,您可以自己解析标题,例如:
<?php
$headers=imap_fetchheader($imap, $msgid);
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $headers, $matches);
?>
$matches will contain 3 arrays:
$matches 将包含 3 个数组:
$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])
Got this from: http://www.php.net/manual/en/function.imap-fetchheader.php#82339
来自:http: //www.php.net/manual/en/function.imap-fetchheader.php#82339
回答by UltimateCodeWarrior
Had same issue as you....had to piece it together, don't know why it's such gonzoware.
和你有同样的问题......不得不拼凑起来,不知道为什么它是这样的gonzoware。
Untested example here:
此处未经测试的示例:
$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);
回答by walter1957
Had trouble until I spotted that the $header is an arrayof stdClass Objects. The following 2 lines worked:
遇到了麻烦,直到我发现 $header 是一个stdClass 对象数组。以下两行有效:
$header=imap_fetch_overview($imap,$countClients,FT_UID);
$strAddress_Sender=$header[0]->from;
回答by Eugene Lycenok
Full working code with an online example
带有在线示例的完整工作代码
Extract email addresses list from inbox using PHP and IMAPinbox-using-php-and-imap
使用 PHP 和 IMAP 从收件箱中提取电子邮件地址列表inbox-using-php-and-imap
I think all you need is just to copy the script.
我认为您所需要的只是复制脚本。
I am publishing two core functions of the code here as well (thanks to Eineki's comment)
我也在这里发布代码的两个核心功能(感谢 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"
);
}
回答by Ben
imap_fetch_overview
could be what you're looking for: http://www.php.net/manual/en/function.imap-fetch-overview.php
imap_fetch_overview
可能是你要找的:http: //www.php.net/manual/en/function.imap-fetch-overview.php
An example of use can be found here: http://davidwalsh.name/gmail-php-imap, specifically
可以在此处找到使用示例:http: //davidwalsh.name/gmail-php-imap,特别是
echo $overview[0]->from;
echo $overview[0]->from;
This function is simple, but has limitations. A more exhaustive version is in imap_headerinfo
( http://www.php.net/manual/en/function.imap-headerinfo.php) which can return detailed arrays of all header data.
这个功能很简单,但是有局限性。更详尽的版本在imap_headerinfo
( http://www.php.net/manual/en/function.imap-headerinfo.php) 中,它可以返回所有标头数据的详细数组。