How to skip the processing of email attachments, which is an attachment of another email address

using jython

I have a situation where emails come with various attachments. I process some types of files others. I ignore and do not write to the file. I found myself in a rather unpleasant situation, because sometimes people send an email as an attachment, and the attached letter has legal attachments.

What I want to do is skip the attached letter and all its attachments.

using python / jythons std email lib how can i do this?


to make it understandable

I need to parse an email (named ROOT email), I want to receive attachments from this email using jython. Further, certain attachments are supported, i.e. .pdf.doc, etc. now it just so happens that clients send an email (ROOT email) with another email (CHILD email) as an attachment, and in the CHILD email it has .pdf attachments, etc.

I need to: get rid of any CHILD emails attached to ROOT email and CHILD email attachments. What happens, I look through all the email and it just analyzes all the attachments, TURNOVO attachments and attachments for the children, as if they were ROOT attachments.

I can’t do that. I'm only interested in ROOT certificates that are legal, i.e...pdf.doc. xls.rtf.tif.tiff

It has to be done now, I have to run to catch the bus! thanks!

+3
source share
4 answers

The problem with existing sentences is the walking method. It recursively, in depth, goes all over the tree, including children.

Look at the source of the walking method and adjust it to skip the recursive part. The cursory readings say:

if msg.is_multipart(): for part in msg.get_payload(): """ Process message, but do not recurse """ filename = part.get_filename() 

Reading pydocs, get_payload should return a list of top-level messages without recursion.

+1
source

What about an example with the name Here is an example of how to unzip a MIME message like above into a file directory "? It is close to what you want.

 import email ... msg = email.message_from_file(fp) ... for part in msg.walk(): # multipart/* are just containers if part.get_content_maintype() == 'multipart': continue # Applications should really sanitize the given filename so that an # email message can't be used to overwrite important files filename = part.get_filename() if not filename: ext = mimetypes.guess_extension(part.get_content_type()) ... 
0
source

Have you tried the get_payload ([i [, decode]]) method? Unlike walking, it is not documented to open attachments recursively.

0
source

I understand that your questions mean "I need to check all email attachments, but if the attachment is also an email, I want to ignore it." In any case, this answer should lead you to the right path.

I think you need mimetypes.guess_type() . Using this method is also much better than just checking the exception list.

 def check(self, msg): import mimetypes for part in msg.walk(): if part.get_filename() is not None: filenames = [n for n in part.getaltnames() if n] for filename in filenames: type, enc = mimetypes.guess_type(filename) if type.startswith('message'): print "This is an email and I want to ignore it." else: print "I want to keep looking at this file." 

Please note that if it is still being viewed through the attached letters, change it to this:

 def check(self, msg): import mimetypes for part in msg.walk(): filename = part.get_filename() if filename is not None: type, enc = mimetypes.guess_type(filename) if type.startswith('message'): print "This is an email and I want to ignore it." else: part_filenames = [n for n in part.getaltnames() if n] for part_filename in part_filenames: print "I want to keep looking at this file." 

MIME Type Documentation

0
source

All Articles