Zend_Mail_Part:获取附件数量

时间:2012-03-01 14:20:51

标签: zend-framework imap email-attachments zend-mail

我怎样才能知道邮件有多少个附件?

这种方法可靠吗?

$attachments = 0;
$msg = $mail->getMessage($msgno);
if($msg->isMultipart()){
    $parts = $msg->countParts();
    for($i=1; $i<=$parts; $i++){
        $part = $msg->getPart($i);
        try {
            if(strpos($part->contentType,'text/html')===false && strpos($part->contentType,'text/plain')===false)
                $attachments++;
        } catch (Zend_Mail_Exception $e) {}
    }
}

还是这个?

$matches = array();
$pattern = '`Content-Disposition: (?!inline)(.*)[\s]*filename=`';
$attachments = (string) preg_match_all($pattern, $storage->getRawContent($msgno), $matches);

1 个答案:

答案 0 :(得分:3)

可能有text/htmltext/plain的附件,因此在所有情况下都可能不可靠。例如,如果附件是HTML文件,则可能出现这种情况。

最好还是检查每个mime部分的content-disposition

$attachments = 0;
$msg = $mail->getMessage($msgno);
if($msg->isMultipart()){
    foreach($msg->getParts() as $part) {
        try {
            if ($part->disposition == Zend_Mime::DISPOSITION_ATTACHMENT || 
                $part->disposition == Zend_Mime::DISPOSITION_INLINE)
                $attachments++;
        } catch (Zend_Mail_Exception $e) {}
    }
}