【问题标题】:How to use Python smtplib to monitor if there is new email received in the mailbox如何使用Python smtplib 监控邮箱是否有新邮件收到
【发布时间】:2021-04-12 08:18:09
【问题描述】:

我正在使用 smtplib 来自动接收电子邮件。以下是我到目前为止所取得的成就

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).

【问题讨论】:

    标签: python smtplib poplib


    【解决方案1】:

    你可以使用睡眠,然后它会每分钟检查收件箱

    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)

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 2020-04-17
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    相关资源
    最近更新 更多