0

I am using smtplib to automate the receiving of emails. Below is what I have achieved so far

import smtplib,ssl
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
import poplib
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
from email.header import Header

    def get_email(email,password,path):
    
        server = poplib.POP3_SSL('pop.' + email.split('@')[-1])  
        server.user(email)
        server.pass_(password)
        resp, mails, octets = server.list()  
        index = len(mails)  
    
        for x in reversed(range(index-2, index+1)):
            server=poplib.POP3_SSL('pop.'+email.split('@')[-1])  
            server.user(email)
            server.pass_(password)
            # resp, mails, octets = server.list() 
            # index = len(mails)       
            # print("total emails: ", index)
            resp, lines, octets = server.retr(x) 
            msg_content = b'\r\n'.join(lines).decode('utf-8','ignore')
            msg = Parser().parsestr(msg_content)
            #server.dele(index) 
            get_header(msg)  
            get_file(path,msg)   
            get_content(msg) 
            server.quit()
            print('receive the lastest',index-x+1, "email")
            print('----------------------------'"\n")

Above function would only start to receive email if the program is activated manually, or with a pre-set timer (e.g. activate the function every 5 minutes). What I need is a more smart function which can help to automatically check if any new emails received in the mailbox, and if yes it follows what is defined in the function to receive and process the email(s).
Jack
  • 1,339
  • 1
  • 12
  • 31

1 Answers1

0

you can use sleep, then it will check inbox every minute

import smtplib,ssl
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
import poplib
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
from email.header import Header
import time

def get_email(email,password,path):

    server = poplib.POP3_SSL('pop.' + email.split('@')[-1])  
    server.user(email)
    server.pass_(password)
    resp, mails, octets = server.list()  
    index = len(mails)  

    for x in reversed(range(index-2, index+1)):
        server=poplib.POP3_SSL('pop.'+email.split('@')[-1])  
        server.user(email)
        server.pass_(password)
        # resp, mails, octets = server.list() 
        # index = len(mails)       
        # print("total emails: ", index)
        resp, lines, octets = server.retr(x) 
        msg_content = b'\r\n'.join(lines).decode('utf-8','ignore')
        msg = Parser().parsestr(msg_content)
        #server.dele(index) 
        get_header(msg)  
        get_file(path,msg)   
        get_content(msg) 
        server.quit()
        print('receive the lastest',index-x+1, "email")
        print('----------------------------'"\n")
for i in range(0,1440):
    get_email(email,password,path)
    time.sleep(60)
Jarry Pan
  • 1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 01 '22 at 04:30