我正在尝试构建一个小型的网络邮件应用程序。当我阅读收件箱中的所有电子邮件时,我想显示每封邮件是否有附件。这是有效的,但问题是这样做需要很长时间,1Mb的电子邮件连接大约需要0.5秒。将其与收件箱中所有具有大附件的电子邮件相乘:|我的问题是:如何在不加载整个电子邮件的情况下检查电子邮件是否已附加?这可能吗?Bellow是我现在使用的代码:
function existAttachment($part)
{
if (isset($part->parts))
{
foreach ($part->parts as $partOfPart)
{
$this->existAttachment($partOfPart);
}
}
else
{
if (isset($part->disposition))
{
if ($part->disposition == 'attachment')
{
echo '<p>' . $part->dparameters[0]->value . '</p>';
// here you can create a link to the file whose name is $part->dparameters[0]->value to download it
return true;
}
}
}
return false;
}
function hasAttachments($msgno)
{
$struct = imap_fetchstructure($this->_connection,$msgno,FT_UID);
$existAttachments = $this->existAttachment($struct);
return $existAttachments;
}
要检查电子邮件是否有附件,请使用$structure->parts[0]->parts。
$inbox = imap_open($mailserver,$username, $password, null, 1, ['DISABLE_AUTHENTICATOR' => 'PLAIN']) or die(var_dump(imap_errors()));
$unreadEmails = imap_search($inbox, 'UNSEEN');
$email_number = $unreadEmails[0];
$structure = imap_fetchstructure($inbox, $email_number);
if(isset($structure->parts[0]->parts))
{
// has attachment
}else{
// no attachment
}
imap_fetchstructure
确实会获取整个电子邮件内容进行分析。遗憾的是,没有其他方法可以检查附件。
也许您可以使用来自imap_headerinfo
的消息大小信息来预测消息是否会有附件。
另一种方法是在后台每隔一段时间提取电子邮件,并将其与内容和UID一起存储,以便稍后在数据库中查找。不管怎样,当你想搜索特定的消息时,你都需要稍后再做。(搜索"晚餐"时,您不想扫描while imap帐户)