I want to retrieve the invalid emails from mail delivery notification sent to my email endpoint from Amazon SES. I don't know how to give arguments to imap4.search() and imap4.fetch() methods in the imaplib module.
Help please
I want to retrieve the invalid emails from mail delivery notification sent to my email endpoint from Amazon SES. I don't know how to give arguments to imap4.search() and imap4.fetch() methods in the imaplib module.
Help please
import imaplib class EmailHandler:
def __init__(self):
self.imap_address = 'imap.gmail.com'
self.login_id = 'example@gmail.com'
self.password = '******'
self.inbox = 'desired inbox'
self.addresses = []
self.M = imaplib.IMAP4_SSL(self.imap_address)
def login(self):
try:
self.M.login(self.login_id, self.password)
self.M.select(self.inbox)
except imaplib.IMAP4.error:
print sys.exc_info()
def extract_invalid_ids(self):
val, data = self.M.search(None, '(OR BODY "bounce" BODY "complaint")')
if val == 'OK':
for num in data[0].split():
val, data = self.M.fetch(num, '(RFC822.TEXT)')
body = data[0][1]
body_json_string = body.split('}}')[0] + '}}'
body_json = json.loads(body_json_string)
self.addresses.extend(body_json['mail']['destination'])
print self.addresses
else:
print val
reference: http://www.isi.edu/in-notes/rfc3501.txt