[英]Find E-Mails with an exact e-mail address on an IMAP server using PHP
我想連接到 IMAP 服務器並找到發送到abc@server.tld
所有電子郵件。 我試過了:
$mbox = imap_open("{imap.server.tld/norsh}", "imap@server.tld", "5ecure3");
$result = imap_search($mbox, "TO \"abc@server.tld\"", SE_UID);
但這也列出了發送到例如123abc@server.tld
。 是否有可能以某種方式搜索完全匹配?
簡短的回答:你不能。 我在RFC 2060 - Internet Message Access Protocol - Version 4rev1 中沒有找到任何說可以做到的內容。
但是,有一個解決方法。 首先獲取包含abc@server.tld
所有電子郵件,然后遍歷結果並僅選擇完全匹配的。
$searchEmail = "abc@server.tld";
$emails = imap_search($mbox, "TO $searchEmail");
$exactMatches = array();
foreach ($emails as $email) {
// get email headers
$info = imap_headerinfo($mbox, $email);
// fetch all emails in the TO: header
$toAddresses = array();
foreach ($info->to as $to) {
$toAddresses[] = $to->mailbox . '@' . $to->host;
}
// is there a match?
if (in_array($searchEmail, $toAddresses)) {
$exactMatches[] = $email;
}
}
現在您在$exactMatches
擁有與abc@server.tld
匹配的所有電子郵件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.