[英]Retrieve email address from imap header
我正在使用php imap函數imap_header
提取完整標題,並使用imap_fetch_overview
提取原始標題。 這兩個函數分別給了我stdClass對象和數組。
我希望在進行進一步處理之前始終清潔我的身體。 有時FROM或TO可能包含這樣的內容,請Test user <test@test.com>
目前在我的“發件人”中,只有在使用Firebug之前,我只會看到Test User
而沒有電子郵件地址。
我如何從這些對象,數組中獲取test@test.com?
這是我從imap_fetch_overview
獲得的結果
Array
(
[0] => stdClass Object
(
[subject] => Testing
[from] => Test User
[to] => testuser2@test.com
[date] => Wed, 17 Apr 2013 18:43:46 +0530
[message_id] => <abcdef1244.93784jgsfk@test.com>
[size] => 3443
[uid] => 1234
[msgno] => 123
[recent] => 0
[flagged] => 0
[answered] => 0
[deleted] => 0
[seen] => 0
[draft] => 0
[udate] => 1366204439
)
)
測試用戶旁邊有一個隱藏的<test@test.com>
。 如何提取該電子郵件地址?
同樣,這是我從imap_header
獲得的imap_header
stdClass Object
(
[date] => Wed, 17 Apr 2013 18:43:46 +0530
[Date] => Wed, 17 Apr 2013 18:43:46 +0530
[subject] => Test
[Subject] => Test
[message_id] => <abcdef1244.93784jgsfk@test.com>
[toaddress] => testuser@test.com
[to] => Array
(
[0] => stdClass Object
(
[mailbox] => testuser
[host] => test.com
)
)
[fromaddress] => Test User
[from] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[reply_toaddress] => Test User
[reply_to] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[senderaddress] => Test User
[sender] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[Recent] =>
[Unseen] => U
[Flagged] =>
[Answered] =>
[Deleted] =>
[Draft] =>
[Msgno] => 123
[MailDate] => 17-Apr-2013 13:13:59 +0000
[Size] => 3443
[udate] => 1366204439
)
preg_match
是一個很明顯的答案,但是似乎無法從瀏覽器中看不到但在用firebug檢查時出現的電子郵件地址中找出如何執行它。 任何幫助表示贊賞。
謝謝。
您可以使用imap_rfc822_parse_adrlist()
Docs更進一步:
$toAddresses = imap_rfc822_parse_adrlist('Test user <test@test.com>', 'localhost');
print_r($toAddresses);
Array
(
[0] => stdClass Object
(
[mailbox] => test
[host] => test.com
[personal] => Test user
)
)
如果std對象數組的名稱為$ arr
$arr[0]->to
返回您想要的。
在第二種情況下
$ stdObject <-第二個函數返回的對象
foreach($stdObject->to as $to)
{
echo $to->mailbox;
echo $to->host;
}
我剛剛發現了這個線程,盡管它已經很舊了,但我希望此評論可能有所幫助。 我相信我已經回答了這個問題。 碰巧的是,發件人電子郵件的格式是導致此問題的原因。
From Name <from@name.com>
由於<>標記,瀏覽器正在將其放入HTML標記,我想這類似於跨度。 通過使用str_replace將“ <”更改為“”(或其他任何內容),您可以在問題到達HTML之前將其刪除
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.