繁体   English   中英

如何在不使用Python触及附件的情况下有效地解析电子邮件

[英]How to efficiently parse emails without touching attachments using Python

我正在使用Python imaplib(Python 2.6)来从GMail获取电子邮件。 我用方法http://docs.python.org/library/imaplib.html#imaplib.IMAP4.fetch获取电子邮件的所有内容我收到完整的电子邮件。 我只需要文本部分,也可以解析附件的名称,而无需下载它们。 怎么做到这一点? 我看到GMail返回的电子邮件遵循浏览器发送到HTTP服务器的相同格式。

看看这个食谱: http//code.activestate.com/recipes/498189/

我稍微调整了它以打印From,Subject,Date,附件名称和消息正文(现在只是纯文本 - 添加HTML消息很简单)。

在这种情况下我使用了Gmail pop3服务器,但它也适用于IMAP。

import poplib, email, string

mailserver = poplib.POP3_SSL('pop.gmail.com')
mailserver.user('recent:YOURUSERNAME') #use 'recent mode'
mailserver.pass_('YOURPASSWORD') #consider not storing in plaintext!

numMessages = len(mailserver.list()[1])
for i in reversed(range(numMessages)):
    message = ""
    msg = mailserver.retr(i+1)
    str = string.join(msg[1], "\n")
    mail = email.message_from_string(str)

    message += "From: " + mail["From"] + "\n"
    message += "Subject: " + mail["Subject"] + "\n"
    message += "Date: " + mail["Date"] + "\n"

    for part in mail.walk():
        if part.is_multipart():
            continue
        if part.get_content_type() == 'text/plain':
            body = "\n" + part.get_payload() + "\n"
        dtypes = part.get_params(None, 'Content-Disposition')
        if not dtypes:
            if part.get_content_type() == 'text/plain':
                continue
            ctypes = part.get_params()
            if not ctypes:
                continue
            for key,val in ctypes:
                if key.lower() == 'name':
                    message += "Attachment:" + val + "\n"
                    break
            else:
                continue
        else:
            attachment,filename = None,None
            for key,val in dtypes:
                key = key.lower()
                if key == 'filename':
                    filename = val
                if key == 'attachment':
                    attachment = 1
            if not attachment:
                continue
            message += "Attachment:" + filename + "\n"
        if body:
            message += body + "\n"
    print message
    print

这应该足以让你朝着正确的方向前进。

通过执行以下操作,您只能获得电子邮件的纯文本:

connection.fetch(id, '(BODY[1])')

对于我见过的gmail消息,第1节有明文,包括多部分垃圾。 这可能不那么强大。

我不知道如何在没有全部的情况下获得附件的名称。 我没有尝试过使用partials。

我怕你运气不好。 根据这篇文章 ,电子邮件只有两个部分 - 标题和正文。 身体是附件所在的位置,如果有任何附件,则必须在仅提取消息文本之前下载整个身体。 此处找到的有关FETCH命令的信息也支持此观点。 虽然它说你可以提取身体的部分,但是这些都是用八位字节来指定的,这并没有真正帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM