我正在尝试通过其标题找到发送电子邮件的内容,然后在内容中搜索某些内容。
import email, imaplib, re, quopri
username = ""
password = ""
mail = imaplib.IMAP4_SSL("imap.mail.yahoo.com", port=993)
mail.login(username, password)
mail.list()
success, emai = mail.search(None, '(SUBJECT "check this out")')
for x in emai[0].split():
mai, dat = mail.fetch(x, "(RFC822)")
decoded = quopri.decodestring(dat[0][1].decode())
email_msg = email.message_from_string(decoded)
break
link = re.search(r'test1 (.*?) test2', str(email_msg))
print(link)
显然我的代码很乱,但这个示例应该显示帮助我所需的一切。例如,我发送了一封名为“check this out”的电子邮件,其中包含“test1 hit test2”作为其内容。然而,我一直在努力解决问题,所以我要求你们帮忙。如何搜索电子邮件然后获取其内容?
谢谢!
for x in emai[0].split():
mai, dat = mail.fetch(x, "(RFC822)")
email_msg = email.message_from_string(dat[0][1])
if email_msg.is_multipart():
for payload in email_msg.get_payload(decode=True):
link = re.search(r'test1 (.*?) test2', payload.get_payload(decode=True))
print(link)
else:
link = re.search(r'test1 (.*?) test2', email_msg.get_payload(decode=True))
print(link)
break