gpt4 book ai didi

Python:不断检查新电子邮件并提醒更多新电子邮件

转载 作者:行者123 更新时间:2023-12-02 03:39:38 26 4
gpt4 key购买 nike

我有这段代码,可以检查最新的电子邮件,然后执行某些操作。是否可以编写一些内容来不断检查收件箱文件夹中是否有新邮件?尽管我希望它继续检查最新的新电子邮件。如果我尝试存储它已经通过了一次,它会变得太复杂吗?因此,它不会针对同一封电子邮件两次发出有关同一封电子邮件的警报。

代码:

import imaplib
import email
import Tkinter as tk

word = ["href=", "href", "<a href="] #list of strings to search for in email body

#connection to the email server
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('xxxx', 'xxxx')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("Inbox", readonly=True) # connect to inbox.

result, data = mail.uid('search', None, "ALL") # search and return uids instead

ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_uid = data[0].split()[-1]

result, data = mail.uid('fetch', latest_email_uid, '(RFC822)') # fetch the email headers and body (RFC822) for the given ID


raw_email = data[0][1] # here's the body, which is raw headers and html and body of the whole email
# including headers and alternate payloads

.....goes and does other code regarding to email html....

最佳答案

尝试使用这种方法:

逻辑与@tripleee评论相同。

import time
word = ["href=", "href", "<a href="] #list of strings to search for in email body

#connection to the email server
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('xxxx', 'xxxx')
mail.list()
# Out: list of "folders" aka labels in gmail.
latest_email_uid = ''

while True:
mail.select("Inbox", readonly=True)
result, data = mail.uid('search', None, "ALL") # search and return uids instead
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string

if data[0].split()[-1] == latest_email_uid:
time.sleep(120) # put your value here, be sure that this value is sufficient ( see @tripleee comment below)
else:
result, data = mail.uid('fetch', latest_email_uid, '(RFC822)') # fetch the email headers and body (RFC822) for the given ID
raw_email = data[0][1]
latest_email_uid == data[0].split()[-1]
time.sleep(120) # put your value here, be sure that this value is sufficient ( see @tripleee comment below)

关于Python:不断检查新电子邮件并提醒更多新电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com