[英]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()
I want the FROM: section from header only. 我只需要标题中的FROM:部分。 Not full name also.
也不全名。 I am now getting data returned as "First name LAst NAME" email@email.com the way I want the data is email@email.com
我现在以我希望数据为email@email.com的方式,以“姓氏名字”的电子邮件地址返回数据
What you want is the envelope
data item, not body.peek[header.fields (...)]
. 您需要的是
envelope
数据项,而不是body.peek[header.fields (...)]
。 When you ask for envelope
, the server does mucho parsing and gives you From, Subject and a few more. 当您请求
envelope
,服务器会进行大量解析,并为您提供From,Subject等信息。 In the case of From you get a list of tuples, each of which may look like this: ("Google Play" NIL "googleplay-noreply" "google.com"). 对于“发件人”,您将获得一个元组列表,每个元组可能看起来像这样:(“ Google Play” NIL“ googleplay-noreply”“ google.com”)。 The first is the name you don't care about, the second is of historical interest only, and the third and fourth are what you want.
第一个是您不关心的名称,第二个仅具有历史意义,第三个和第四个是您想要的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.