【问题标题】:Python: Keep checking new email and alert of further new emailsPython:不断检查新电子邮件并提醒更多新电子邮件
【发布时间】:2018-08-11 16:36:19
【问题描述】:

我有这段代码可以检查最新的电子邮件,然后去做一些事情。是否可以写一些东西来不断检查收件箱文件夹是否有新邮件?虽然我希望它继续检查最新的新电子邮件。如果我尝试存储它已经通过一次,它会变得太复杂吗?因此,它不会针对同一封电子邮件提醒同一封电子邮件两次。

代码:

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

【问题讨论】:

  • 在伪代码中,当为真时:获取最新消息;如果它比以前更新,则显示警报;以前的 := 最新的;睡觉;
  • Python 的 imaplib 不支持 IMAP IDLE 命令,但最好使用它。
  • IIRC 你应该能够摆脱一些比总是“搜索所有”更残酷的事情,但现在不是在 / 好地方检查。

标签: python email imaplib


【解决方案1】:

尝试使用这种方法:

逻辑与@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)

【讨论】:

  • 你想睡的时间明显长于五秒;许多 IMAP 服务器管理员已经认为每分钟一次是相当激进的。根据您的应用程序和预期流量,每五分钟或十分钟检查一次可能就足够了。
  • @tripleee 好的,我会改的 :) 我不熟悉 IMAP imaplib,所以这段代码只是我读完问题后的想法。
  • 我需要重组 else 语句以使一切正常,然后运行我的命令。按此顺序 - latest_email_uid == data[0].split()[-1] > 结果,data = mail.uid('fetch', latest_email_uid, '(RFC822)') > raw_email = data[0][1]它运行并等待它似乎再次运行时出现此错误.....在下一条评论中......
  • 回溯(最近一次调用最后):文件“beta_1.4.py”,第 36 行,在 结果中,data = mail.uid('search', None, "ALL") # 搜索并返回 uids File "C:\Python27\lib\imaplib.py", line 773, in uid ', '.join(Commands[command])))在选定的州中允许
  • @idwithin 你能把mail.select("Inbox", readonly)移动到while True里面吗?放在while True 语句之后的第一行。
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2019-07-14
  • 1970-01-01
  • 2018-12-09
  • 2021-10-22
  • 1970-01-01
  • 2022-06-16
相关资源
最近更新 更多