简体   繁体   English

来自Gmail帐户的未读电子邮件

[英]Unread e-mails from Gmail account

I have a python script as follows: 我有一个python脚本,如下所示:

import imaplib, re
import os
import time
import socket

imap_host = 'imap.gmail.com'
mail = imaplib.IMAP4_SSL(imap_host)
mail.login("user", "pass")
mail.select("inbox") # connect to inbox.

while True:
    try:
        result, data = mail.uid('search', None, 'UNSEEN')
        uid_list = data[0].split()
        print len(uid_list), 'Unseen emails.'
        if len(uid_list) > 20:
         os.system('heroku restart --app xxx')
        time.sleep(60)
    except KeyboardInterrupt:
        print 'Quitting'
        pass

The goal is to check how many unread emails there is and restart a Heroku server. 目的是检查有多少未读电子邮件并重新启动Heroku服务器。 The problem is that it doesn't seem to work very well. 问题在于它似乎不能很好地工作。

~# python gmail_new.py
1 Unseen emails.
0 Unseen emails.

When I start the first time, it works (1 unread email), but the second time it should have returned 20 and not 0. And next is also 0, never more than 0. I can't seem to figure out why. 第一次启动时,它可以工作(1封未读的电子邮件),但是第二次它应该返回20而不是0。下一次也是0,从不大于0。我似乎无法弄清楚为什么。

As far as I can tell, you get one extra unseen email all the time. 据我所知,您总是会收到一封额外的看不见的电子邮件。 Make sure you have a few unread mails in your account. 确保您的帐户中有一些未读邮件。 replace : 替换:

print len(uid_list), 'Unseen emails.'

with

print len(uid_list)-1, 'Unseen emails.'

this should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM