Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to filter emails that have attachments, but in the system I'm using I only have access to the headers (for filtering). Is there a consistent way of determining if an e-mail contains attachments using only headers? (Perhaps checking if the content-type is multipart?)

share|improve this question
Checking for mime-type='multipart/mixed' works in all emails that I've seen with Gmail. This is if you define an attachment by what Gmail shows a paperclip for. – wezten Jul 20 at 9:15
up vote 5 down vote accepted

You cannot achieve your desired goal if you have access only to the headers. The information may be there as a non-standard header but that's obviously not something that you can rely on. Either get access to the body or abandon the idea.

share|improve this answer

Mails without attachments may be content-type:multipart too, like those with text and html versions of the content. What you would have to check is c-t:multipart and then for parts that are not content-type:text/*, but for that you will need access to the body :(

share|improve this answer

For Mime attachments, emails with a Content-Type: multipart/mixed would often have attachments (but don't have to). Emails with a Content-Type: other than multipart/* would not have attachments. Anything else (multipart/alternative being the most common) may have attachments.

And in any case, that depends what is meant by attachment. If by attachment, you mean only those mime parts that have a attachment Content-Disposition, then you won't find it out in the email header, only in the corresponding part mime headers.

Things you may not want to consider as attachments are for instance email signatures, parts of a multipart/related message that are for instance images embedded in the signature of an html message...

share|improve this answer

I got email header using belowe code:

 msg_header = @imap.fetch(message_id, 'RFC822.HEADER').first.attr['RFC822.HEADER']
headers = msg_header.split(/\r\n|: /)
email_header = headers[headers.index('Content-Type')+1].split(/;/)

Then you can decide on header this mail has attachment or not without get body.

you can check header information from here Hope this helps you.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.