I want to access the "TO" field in the email object in python which appears at the end of a failed-email notification
---------- Forwarded message ---------- From: test email val1testemail@gmail.com To: no_such_email@gmail.com Cc: Bcc: Date: Mon, 25 Apr 2022 09:06:05 -0400 Subject: test test
# select specific mails
_, selected_mails = mail.search(None, '(FROM "mailer-daemon@googlemail.com")')
#total number of mails from specific user
print("Total Messages from :mailer-daemon@googlemail.com" , len(selected_mails[0].split()))
#print(_,selected_mails[0].split())
for num in selected_mails[0].split():
_, data = mail.fetch(num , '(RFC822)')
_, bytes_data = data[0]
#convert the byte data to message
email_message = email.message_from_bytes(bytes_data)
print("\n===========================================")
#access data
print("Subject: ",email_message["subject"])
print("To:", email_message["to"])
print("From: ",email_message["from"])
print("Date: ",email_message["date"])
print(email_message.get_all("To",_))
#this statement says there is only one TO field which is not the one I want.
source https://stackoverflow.com/questions/72005316/i-want-to-access-the-to-field-in-the-email-object-in-python-which-appears-at-t
Comments
Post a Comment