I've been trying to implement this script, it polls an IMAP inbox for
unread messages and displays the sender and subject in a scrollable
window using Tkinter. However, when I try to change the search
parameters on line 55 from 'unread' (UNSEEN) to 'read' (SEEN), the
tkinter window doesn't even pop up anymore.
Any idea how to change the parameters of this program and yet still keep
it functional?
my code: (if you can't see line 55, search for 'UNSEEN')
#!/usr/bin/env python
import imaplib, string, sys, os, re, rfc822
from Tkinter import *
PollInterval = 60 # seconds
def getimapaccount( ):
try:
f = open(('info.ima p'))
except IOError, e:
print 'Unable to open ~/.imap: ', e
sys.exit(1)
global imap_server, imap_user, imap_password
try:
imap_server, imap_user, imap_password = string.split(f. readline())
except ValueError:
print 'Invalid data in ~/.imap'
sys.exit(1)
f.close()
class msg: # a file-like object for passing a string to rfc822.Message
def __init__(self, text):
self.lines = string.split(te xt, '\015\012')
self.lines.reve rse()
def readline(self):
try: return self.lines.pop( ) + '\n'
except: return ''
class Mailwatcher(Fra me):
def __init__(self, master=None):
Frame.__init__( self, master)
self.pack(side= TOP, expand=YES, fill=BOTH)
self.scroll = Scrollbar(self)
self.list = Listbox(self, font='7x13',
yscrollcommand= self.scroll.set ,
setgrid=1, height=6, width=80)
self.scroll.con figure(command= self.list.yview )
self.scroll.pac k(side=LEFT, fill=BOTH)
self.list.pack( side=LEFT, expand=YES, fill=BOTH)
def getmail(self):
self.after(1000 *PollInterval, self.getmail)
self.list.delet e(0,END)
try:
M = imaplib.IMAP4(i map_server)
M.login(imap_us er, imap_password)
except Exception, e:
self.list.inser t(END, 'IMAP login error: ', e)
return
try:
result, message = M.select(readon ly=1)
if result != 'OK':
raise Exception, message
typ, data = M.search(None, '(UNSEEN)')
for num in string.split(da ta[0]):
try:
f = M.fetch(num, '(BODY[HEADER.FIELDS (SUBJECT FROM)])')
m = rfc822.Message( msg(f[1][0][1]), 0)
subject = m['subject']
except KeyError:
f = M.fetch(num, '(BODY[HEADER.FIELDS (FROM)])')
m = rfc822.Message( msg(f[1][0][1]), 0)
subject = '(no subject)'
fromaddr = m.getaddr('from ')
if fromaddr[0] == "": n = fromaddr[1]
else: n = fromaddr[0]
text = '%-20.20s %s' % (n, subject)
self.list.inser t(END, text)
len = self.list.size( )
if len > 0: self.list.see(l en-1)
except Exception, e:
self.list.delet e(0,END)
print sys.exc_info()
self.list.inser t(END, 'IMAP read error: ', e)
M.logout()
getimapaccount( )
root = Tk(className='m ailwatcher')
root.title('mai lwatcher')
mw = Mailwatcher(roo t)
mw.getmail()
mw.mainloop()
unread messages and displays the sender and subject in a scrollable
window using Tkinter. However, when I try to change the search
parameters on line 55 from 'unread' (UNSEEN) to 'read' (SEEN), the
tkinter window doesn't even pop up anymore.
Any idea how to change the parameters of this program and yet still keep
it functional?
my code: (if you can't see line 55, search for 'UNSEEN')
#!/usr/bin/env python
import imaplib, string, sys, os, re, rfc822
from Tkinter import *
PollInterval = 60 # seconds
def getimapaccount( ):
try:
f = open(('info.ima p'))
except IOError, e:
print 'Unable to open ~/.imap: ', e
sys.exit(1)
global imap_server, imap_user, imap_password
try:
imap_server, imap_user, imap_password = string.split(f. readline())
except ValueError:
print 'Invalid data in ~/.imap'
sys.exit(1)
f.close()
class msg: # a file-like object for passing a string to rfc822.Message
def __init__(self, text):
self.lines = string.split(te xt, '\015\012')
self.lines.reve rse()
def readline(self):
try: return self.lines.pop( ) + '\n'
except: return ''
class Mailwatcher(Fra me):
def __init__(self, master=None):
Frame.__init__( self, master)
self.pack(side= TOP, expand=YES, fill=BOTH)
self.scroll = Scrollbar(self)
self.list = Listbox(self, font='7x13',
yscrollcommand= self.scroll.set ,
setgrid=1, height=6, width=80)
self.scroll.con figure(command= self.list.yview )
self.scroll.pac k(side=LEFT, fill=BOTH)
self.list.pack( side=LEFT, expand=YES, fill=BOTH)
def getmail(self):
self.after(1000 *PollInterval, self.getmail)
self.list.delet e(0,END)
try:
M = imaplib.IMAP4(i map_server)
M.login(imap_us er, imap_password)
except Exception, e:
self.list.inser t(END, 'IMAP login error: ', e)
return
try:
result, message = M.select(readon ly=1)
if result != 'OK':
raise Exception, message
typ, data = M.search(None, '(UNSEEN)')
for num in string.split(da ta[0]):
try:
f = M.fetch(num, '(BODY[HEADER.FIELDS (SUBJECT FROM)])')
m = rfc822.Message( msg(f[1][0][1]), 0)
subject = m['subject']
except KeyError:
f = M.fetch(num, '(BODY[HEADER.FIELDS (FROM)])')
m = rfc822.Message( msg(f[1][0][1]), 0)
subject = '(no subject)'
fromaddr = m.getaddr('from ')
if fromaddr[0] == "": n = fromaddr[1]
else: n = fromaddr[0]
text = '%-20.20s %s' % (n, subject)
self.list.inser t(END, text)
len = self.list.size( )
if len > 0: self.list.see(l en-1)
except Exception, e:
self.list.delet e(0,END)
print sys.exc_info()
self.list.inser t(END, 'IMAP read error: ', e)
M.logout()
getimapaccount( )
root = Tk(className='m ailwatcher')
root.title('mai lwatcher')
mw = Mailwatcher(roo t)
mw.getmail()
mw.mainloop()