繁体   English   中英

仅使用python中的imap解析电子邮件

[英]Parsing e-mail only with imap in python

conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login("login", "pass")
conn.select()
typ, data = conn.search(None, 'ALL')
z = open("email.txt", "a")

for num in data[0].split():
    typ, msg_data = conn.fetch(num, '(BODY[HEADER.FIELDS (SUBJECT FROM)])')
    for response_part in msg_data:
        if isinstance(response_part, tuple):
            msg = email.message_from_string(response_part[1])
            subject = msg['from']
            z.write("%s\n" % subject) 
            print(subject)

    typ, response = conn.store(num, '+FLAGS', r'(\Seen)')
finally:
try:
    conn.close()
except:
    pass
conn.logout()

我只需要标题中的FROM:部分。 也不全名。 我现在以我希望数据为email@email.com的方式,以“姓氏名字”的电子邮件地址返回数据

您需要的是envelope数据项,而不是body.peek[header.fields (...)] 当您请求envelope ,服务器会进行大量解析,并为您提供From,Subject等信息。 对于“发件人”,您将获得一个元组列表,每个元组可能看起来像这样:(“ Google Play” NIL“ googleplay-noreply”“ google.com”)。 第一个是您不关心的名称,第二个仅具有历史意义,第三个和第四个是您想要的名称。

暂无
暂无

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

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