【问题标题】:How to deal with flooded unseen messages如何处理淹没的看不见的消息
【发布时间】:2012-08-31 20:29:45
【问题描述】:

我用python写了一个邮件解析机制。

它会找到一封新电子邮件并正确传递数据。我 99.999% 确定我的代码运行正常,所以那里应该没有问题。问题是有时,Gmail 收件箱会被视为“看不见”的邮件淹没。在这一点上,我的代码无能为力。

它失败了:

imaplib.error: FETCH 命令错误: BAD ['Could not parse command']

这很痛苦,我很想拥有一个

  1. 一种检查看不见的消息是否已溢出到此状态的方法,或者
  2. 一种手动​​(通过 imaplib)将所有消息标记为已读的方法,包括检测此特定错误的方法。

关于如何实现这一点的任何想法?

这是我的代码:

#!/usr/bin/env python

import imaplib, re, sys, time, OSC, threading, os

iparg = 'localhost' 
oportarg = 9000
iportarg = 9002
usern = 'myusrname@gmail.com'
gpass = 'mypass' 
kill_program = False

server = imaplib.IMAP4_SSL('imap.googlemail.com', 993)
oclient = OSC.OSCClient()
email_interval = 2.0

def login():
    server.login(usern, gpass)
    oclient.connect((iparg, oportarg))

def logout_handle(addr, tags, stuff, source):
    print 'received kill call'
    global kill_program
    kill_program = True

def filter_signature(s):  #so annoying; wish i didn't have to do this
    try:
    a_sig = re.sub(r'Sent|--Sent', '', s)
    b_sig = re.sub(r'using SMS-to-email.  Reply to this email to text the sender back and', '', a_sig)
    c_sig = re.sub(r'save on SMS fees.', '', b_sig)
    d_sig = re.sub(r'https://www.google.com/voice', '', c_sig)
    no_lines = re.sub(r'\n|=|\r?', '', d_sig) #add weird characters to this as needed
    except:
    nolines = s
    return no_lines

def parse_email(interval):
    while True:
    server.select('INBOX')
    status, ids = server.search(None, 'UnSeen')
    print 'status is: ', status

    if not ids or ids[0] is '':
        print 'no new messages'
    else:
        try:
        print 'found a message; attempting to parse...'
        latest_id = ids[0]
        status, msg_data = server.fetch(latest_id, '(UID BODY[TEXT])')
        raw_data = msg_data[0][1]
        raw_filter = raw_data

        print 'message result: ', raw_filter
    time.sleep(interval)

#execute main block
while not kill_program:
    login()
    parse_email(email_interval)

st.kill()
sys.exit()

【问题讨论】:

  • 简单评论:每次我看到一个程序员(甚至我自己)确信有 99,9999% 的“正确机会”程序时,我看到程序员有 99,99999 % "出错的可能性"。
  • 好的,谢谢...仍然想知道我是否可以在不发布代码的情况下收集一些用于故障排除的想法。如有必要,我很乐意发布代码。
  • 您没有调用堆栈或因该错误转储的任何内容吗?
  • 1.根据错误,我会非常仔细地检查您传递给 fecth 的参数。 Gmail 告诉您它无法解析您发送给它的命令。 2. 您可以执行 STORE +FLAGS \\SEEN 将消息标记为已读。附:如果你发布你的代码,我会看看。
  • @kkurian,如果您可以将您的评论转换为答案格式,我很乐意将其视为有效答案。

标签: python gmail imap imaplib


【解决方案1】:

根据错误,我会非常仔细地检查您传递给 fetch 的参数。 Gmail 告诉您它无法解析您发送给它的命令。

另外,您可以使用 STORE +FLAGS \SEEN 将消息标记为已读。

【讨论】:

  • 你能告诉我这个代码是什么样的吗?我会想到类似server.store(num, '+FLAGS', 'Seen'),但不确定...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-21
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
相关资源
最近更新 更多