0

I have written an email parsing mechanism in python.

It finds a new email and passes the data correctly. I am 99.999% certain that my code is functioning correctly, so there should be no issue there. The problem is that occasionally, the Gmail inbox will get flooded with messages that are considered "unseen". At this point, there is nothing that my code can do.

It fails with:

imaplib.error: FETCH command error: BAD ['Could not parse command']

This is distressing, and I would love to have either

  1. a way to check whether the unseen messages have overflown to this state, or
  2. a way to manually (via imaplib) mark all messages as read, including a way to detect this particular error.

Any thoughts on how to accomplish this?

Here is my code:

#!/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()
jml
  • 1,745
  • 6
  • 29
  • 55
  • Simple comment : each time I've seen a programmer (even myself) convinced of having of 99,9999% "chances of being correct" program, I've seen that the programmer had 99,99999% "chances of being wrong". – AsTeR Sep 06 '12 at 23:45
  • ok, thanks... still wondering if i can gather some ideas for troubleshooting without posting code. i'd be happy to post code if necessary. – jml Sep 07 '12 at 01:26
  • Don't you have the call stack or anything dumped with that error ? – AsTeR Sep 07 '12 at 06:51
  • 1
    1.Based upon the error, I would very carefully check the parameters that you're passing to fecth. Gmail is telling you that it could not parse the command that you sent to it. 2. You can do a STORE +FLAGS \\SEEN to mark the messages as read. P.S. If you post your code I'll take a look. – kkurian Oct 14 '12 at 04:37
  • sorry that took me forever (whew). i added the code, if you have any time, @kkurian. thanks – jml Dec 31 '12 at 19:00
  • 1
    @kkurian, if you could please put your comment into an answer format, I'd be happy to consider it as a valid answer. – jml Feb 04 '13 at 20:44

1 Answers1

1

Based upon the error, I would very carefully check the parameters that you're passing to fetch. Gmail is telling you that it could not parse the command that you sent to it.

Also, you can do a STORE +FLAGS \SEEN to mark the messages as read.

kkurian
  • 3,844
  • 3
  • 30
  • 49
  • Can you tell me what the code for this looks like? I would think something like `server.store(num, '+FLAGS', 'Seen')`, but not sure... – jml May 10 '13 at 17:55