![](/img/trans.png)
[英]PHP - IMAP - Is it possible to get receivers email address from a sent email?
[英]php imap get from email address
如何從imap_open的電子郵件中檢索電子郵件地址?
如果我知道發件人姓名,我會使用'from'參數獲取發件人姓名而不是電子郵件地址。
代碼: http : //gist.github.com/514207
$header = imap_headerinfo($imap_conn, $msgnum);
$fromaddr = $header->from[0]->mailbox . "@" . $header->from[0]->host;
我也與此作斗爭,但以下工作:
// 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()給了我所需的所有信息。
最糟糕的情況是,您可以使用以下內容解析標題:
<?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
和你一樣的問題......不得不將它拼湊在一起,不知道為什么會這樣的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);
imap_fetch_overview
可能是您正在尋找的: http : imap_fetch_overview
可以在此處找到使用示例: http : //davidwalsh.name/gmail-php-imap ,具體而言
echo $overview[0]->from;
這個功能很簡單,但有局限性。 更詳盡的版本在imap_headerinfo
( http://www.php.net/manual/en/function.imap-headerinfo.php )中,它可以返回所有標題數據的詳細數組。
遇到麻煩,直到我發現$ header是一個stdClass對象數組 。 以下兩行有效:
$header=imap_fetch_overview($imap,$countClients,FT_UID);
$strAddress_Sender=$header[0]->from;
完整的工作代碼與在線示例
使用PHP和IMAP inbox-using-php-and-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"
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.