使用PHP在IMAP服务器上查找具有确切电子邮件地址的电子邮件 [英] Find E-Mails with an exact e-mail address on an IMAP server using PHP

查看:82
本文介绍了使用PHP在IMAP服务器上查找具有确切电子邮件地址的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接到IMAP服务器,并找到所有发送到 abc@server.tld 的电子邮件.我试过了:

I want to connect to an IMAP-server and find all E-Mails that were sent to abc@server.tld. I tried:

$mbox = imap_open("{imap.server.tld/norsh}", "imap@server.tld", "5ecure3");
$result = imap_search($mbox, "TO \"abc@server.tld\"", SE_UID);

,但这还会列出已发送的电子邮件,例如到 123abc@server.tld .是否可以通过某种方式搜索完全匹配?

but this also listed e-Mails that were sent e.g. to 123abc@server.tld. Is it somehow possible to do a search for exact matches?

推荐答案

简短的回答:不能.我在 RFC 2060-Internet邮件访问协议-版本4rev1 中找不到任何内容可以做到的.

Short answer: you can't. I didn't find anything in RFC 2060 - Internet Message Access Protocol - Version 4rev1 saying that it can be done.

但是,有一种解决方法.首先获取所有包含 abc@server.tld 的电子邮件,然后遍历结果并仅选择完全匹配的内容.

But, there is a workaround. First fetch all emails that contain abc@server.tld, then iterate through the results and select only the exact matches.

$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

这篇关于使用PHP在IMAP服务器上查找具有确切电子邮件地址的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆