如何跳过处理另一封电子邮件的附件

发布于 2024-07-09 07:04:39 字数 607 浏览 4 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

高冷爸爸 2024-07-16 07:04:39

现有建议的问题在于步行方法。 这种深度优先的递归方式会遍历整个树,包括子树。

查看walk方法的来源,并对其进行修改以跳过递归部分。 粗略阅读表明:

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

阅读 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:

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.

半步萧音过轻尘 2024-07-16 07:04:39

名为“的示例怎么样?这是一个如何解压上面的 MIME 消息的示例,进入文件目录”? 它看起来与你想要的很接近。

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())
    ...

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.

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())
    ...
×纯※雪 2024-07-16 07:04:39

您是否尝试过 get_payload([i[,decode]]) 方法? 与 walk 不同,它没有记录为递归打开附件。

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

檐上三寸雪 2024-07-16 07:04:39

我理解您的问题的意思是“我必须检查电子邮件的所有附件,但如果附件也是电子邮件,我想忽略它。” 不管怎样,这个答案应该会引导你走上正确的道路。

我认为你想要的是mimetypes.guess_type()。 使用此方法也比仅检查扩展列表要好得多。

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."

请注意,如果仍然查看附加电子邮件,请将其更改为:

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 类型文档

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.

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."

Note that if this still looks through attached emails, 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 types documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文