php imap检查电子邮件是否有附件 [英] php imap check if email has attachment
问题描述
我正在尝试构建一个小型Webmail应用程序.当我阅读收件箱中的所有电子邮件时,如果每个邮件都有附件,我希望显示它们.这行得通,但问题是这样做需要花费很长时间,大约1秒的1Mb电子邮件附加时间为0.5秒.将其与收件箱中具有大附件文件的所有电子邮件相乘:|我的问题是:如何在不加载整个电子邮件的情况下检查电子邮件是否已附加?那可能吗 ?贝娄是我现在正在使用的代码:
I'm trying to build a small webmail app. When I read all the emails in inbox I want to show for each mail if it has attachments. This works, but the problem is that it takes to long to do that, about 0.5 secs for 1Mb email attach. Multiply that with all emails in inbox that have big attach files :| My question is: How to check if an email has attach withouth loading the whole email ? Is that possible ? Bellow is the code I'm using now:
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;
}
推荐答案
imap_fetchstructure
确实会提取整个电子邮件内容以进行分析.遗憾的是,没有其他方法可以检查附件.
imap_fetchstructure
does fetch the whole email content in order to analyze it. Sadly there is no other way to check for attachment.
也许您可以使用 imap_headerinfo
中的邮件大小信息来预测邮件是否包含附件.
Maybe you can use the message size info from imap_headerinfo
to get a prediction if the message will have attachments.
另一种方法是在后台定期提取电子邮件,并将电子邮件及其内容和UID存储起来,以便以后在数据库中查找.无论如何,您以后都需要这样做,以便搜索特定的消息.(搜索晚餐"时,您不想扫描while imap帐户)
Another way is to fetch the emails in an regular interval in the background and store them with their content and UID for later lookup in a database. You need to do that later anyway when you want so search for specific messages. (You do not want to scan the while imap account when searching for "dinner")
这篇关于php imap检查电子邮件是否有附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!