如何跳过处理另一封电子邮件的附件
使用 jython
我遇到了电子邮件带有不同附件的情况。 我处理某些文件类型,而另一些文件类型则忽略并且不写入文件。 我陷入了一个相当糟糕的境地,因为有时人们会以附件的形式发送电子邮件,而该附加电子邮件包含合法的附件。
我想要做的是跳过该附加电子邮件及其所有附件。
使用 python/jythons std email lib 我该怎么做?
为了更清楚地说明
我需要解析一封电子邮件(名为 ROOT 电子邮件),我想使用 jython 从此电子邮件中获取附件。 接下来支持某些附件,即 .pdf .doc 等 现在碰巧的是,客户发送一封电子邮件(ROOT 电子邮件),其中包含另一封电子邮件(子电子邮件)作为附件,并且在子电子邮件中包含 .pdf 附件等。
我需要的是:删除附加到根电子邮件和儿童电子邮件附件的任何儿童电子邮件。 发生的情况是,我浏览了整封电子邮件,它只是解析每个附件,包括根附件和子附件,就好像它们是根附件一样。
我不能拥有这个。 我只对合法的 ROOT 附件感兴趣,即 .pdf .doc。 xls .rtf .tif .tiff
现在应该可以了,我必须跑去赶公共汽车! 谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
现有建议的问题在于步行方法。 这种深度优先的递归方式会遍历整个树,包括子树。
查看walk方法的来源,并对其进行修改以跳过递归部分。 粗略阅读表明:
阅读 pydocs,get_payload 应该返回顶级消息的列表,而不需要递归。
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:
Reading the pydocs, get_payload should return a list of the top level messages, without recursing.
名为“的示例怎么样?这是一个如何解压上面的 MIME 消息的示例,进入文件目录”? 它看起来与你想要的很接近。
What about the example named "Here’s an example of how to unpack a MIME message like the one above, into a directory of files"? It looks close from what you want.
您是否尝试过 get_payload([i[,decode]]) 方法? 与 walk 不同,它没有记录为递归打开附件。
Have you tried the get_payload( [i[, decode]]) method? Unlike walk it is not documented to recursively open attachments.
我理解您的问题的意思是“我必须检查电子邮件的所有附件,但如果附件也是电子邮件,我想忽略它。” 不管怎样,这个答案应该会引导你走上正确的道路。
我认为你想要的是
mimetypes.guess_type()
。 使用此方法也比仅检查扩展列表要好得多。请注意,如果仍然查看附加电子邮件,请将其更改为:
MIME 类型文档
I'm understanding your questions to mean "I have to check all attachments of an email, but if an attachment is also an email, I want to ignore it." Either way this answer should lead you down the right path.
What I think you want is
mimetypes.guess_type()
. Using this method is also much better than just checking against a list of exentions.Note that if this still looks through attached emails, change it to this:
MIME types documentation