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?)
4 Answers
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.
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 :(
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...
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.