Search code examples
phpemailimapattachmentfilesize

PHP get filesize of imap attachments


I am reading imap email with PHP and downloading attachments, using the core imap PHP functions. I am also connecting via the imap protocol (not POP). I would like to be able to know the filesize of attachments before I load them into memory, to reduce server resource usage.

For example I want a limit of 5mb per attachment, this way I can discard any attachments that are over the limit, before they consume any server resources. Finding the size of the email might help but the problem with that is I want the limit per attachment, not per email.

imap_fetchbody() is loading the attachment, which all works. I could probably read the size of the result of that function but that would mean loading the attachment into memory first.

Thanks


Solution

  • I realize this is an older post, but in case it helps, the difference in size is due to the attachments being base64-encoded, which results in a roughly 33% expansion of the data. From RFC3501: "[Body size is] A number giving the size of the body in octets. Note that this size is the size in its transfer encoding and not the resulting size after any decoding."