Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I skip processing the attachments of an email which is an attachment of a different email

using jython

I have a situation where emails come in with different attachments. Certain file types I process others I ignore and dont write to file. I am caught in a rather nasty situation, because sometimes people send an email as an attachment, and that attached email has legal attachments.

What I want to do is skip that attached email and all its attachments.

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


to make it clearer

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

What I need is: to get rid of any CHILD emails attached to the ROOT email AND the CHILD emails attachments. What happens is I walk over the whole email and it just parses every attachment, BOTH ROOT attachments and CHILD attachments as if they were ROOT attachments.

I cannot have this. I am only interested in ROOT attachements that are legal ie .pdf .doc. xls .rtf .tif .tiff

That should do for now, I have to run to catch a bus! thanks!

like image 385
Setori Avatar asked Nov 18 '25 21:11

Setori


1 Answers

The problem with existing suggestions is the walk method. This recursively, depth-first, walks the entire tree, including children.

Look at the source of the walk method, and adapt it to skip the recursive part. A cursory reading suggests:

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

Reading the pydocs, get_payload should return a list of the top level messages, without recursing.

like image 87
jmanning2k Avatar answered Nov 20 '25 12:11

jmanning2k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!