0

I'm trying to make something that finds an email by its title and then searches its content for something.

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)

Obviously my code is messy, but this sample should show everything that's needed to help me. For example, I sent myself an email named "check this out" with "test1 hit test2" as its content. However I've been struggling to figure stuff out, therefore I'm asking you guys for help. How do I search for an email and then fetch its content?

Thank you!

Vaporwave30
  • 59
  • 1
  • 3

1 Answers1

0
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