AHHP
AHHP

Reputation: 3047

Zend_Mail_Part: Get number of attachments

How can i find out that how many attachments a message has?

Is this method reliable?

$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) {}
    }
}

or this?

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

Upvotes: 0

Views: 2392

Answers (1)

drew010
drew010

Reputation: 69977

It is possible to have attachments that are text/html or text/plain, so it may not be reliable in all cases. If there is an attachment that is an HTML file for example, you could have this situation.

You may be better off checking the content-disposition of each mime part instead:

$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) {}
    }
}

Upvotes: 3

Related Questions