I am reading the unread emails from my Inbox. I am getting the total number of unread emails, but I am unable to fetch the number of emails in a single email chain separately. For eg. There are three unread emails(One with a 10 email chain, one with a 4 email chain, and the last one with a 5 email chain). I am getting the output as 19 unread emails which I am correcting but storing the subjects in a pandas dataframe and then taking the unique values only, which gives me 3 unread emails. But I am still not unable to get the number of emails in a single email chain i.e 10, 4 and 5.
I am using the imaplib to get the emails.
mail = imaplib.IMAP4_SSL('imap.gmail.com')
(retcode, capabilities) = mail.login(username,password)
mail.select('Inbox')
n_unread_emails=0
(retcode, messages) = mail.search(None, '(UNSEEN)')
if retcode == 'OK':
for num in messages[0].split() :
n_unread_emails = n_unread_emails + 1
typ, data = mail.fetch(num,'(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
original = email.message_from_string(response_part[1])
alerttrack = alerttrack.append({'Subject' : str(original['Subject']).replace('[Check_mk Alert RCA] ','')}, ignore_index = True)
n_unread_emails = alerttrack.Subject.nunique(dropna = True)
print "Number of Unread Mails are " + str(n_unread_emails)