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:部分。还不是全名。我现在收到的数据是"名字LAst NAME" email@email.com我想要数据的方式是email@email.com
答案 0 :(得分:0)
您想要的是envelope
数据项,而不是body.peek[header.fields (...)]
。当你要求envelope
时,服务器会进行很多解析,并为你提供From,Subject等等。在From的情况下,您会得到一个元组列表,每个元组可能如下所示:(“Google Play”NIL“googleplay-noreply”“google.com”)。第一个是你不关心的名字,第二个是历史唯一的,第三个和第四个是你想要的。