0

Basically I am making a code that saves emails locally as .txt files then apply sentiment analysis on it to find angry customers, my current code saves "TODAY'S" emails locally then do the analysis but what I want to do is just run the script once and let it loop and get the latest email, so every time a new email is received it will apply sentiment analysis on it, otherwise if the latest email is still the same it does nothing.

Here is the part of the code that gets today's emails:

mail.login(email_user, email_pass) # LOG IN.
mail.list()
mail.select("inbox") # Go to Inbox folder

today = datetime.datetime.today() # Today's date.
dt = today.strftime('%d-%b-%Y')


result, data = mail.uid('search', None, '(SINCE %s) '%(dt,)) #Uncomment to get emails from today only.'''
Yasser Attia
  • 31
  • 1
  • 3
  • Generally, if you're dealing with IMAP with Python - it's worth while using the `IMAPClient` library... whether you do that or not, you want to use IDLE and do something similar to https://imapclient.readthedocs.io/en/2.1.0/advanced.html#watching-a-mailbox-using-idle – Jon Clements Jun 10 '19 at 11:00
  • If your email is self-hosted, it's much better to put this in the SMTP receive path, rather than as an IMAP client. You can have your SMTP server run a script on every received email. – Max Jun 10 '19 at 13:51
  • There's a few solutions at https://stackoverflow.com/questions/13210737/get-only-new-emails-imaplib-and-python – Tristu Jun 11 '19 at 01:27

0 Answers0