473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

IMAP mailwatcher w/Tkinter

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.imap'))
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(text, '\015\012')
self.lines.reverse()
def readline(self):
try: return self.lines.pop() + '\n'
except: return ''

class Mailwatcher(Frame):
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.configure(command=self.list.yview)
self.scroll.pack(side=LEFT, fill=BOTH)
self.list.pack(side=LEFT, expand=YES, fill=BOTH)

def getmail(self):
self.after(1000*PollInterval, self.getmail)
self.list.delete(0,END)
try:
M = imaplib.IMAP4(imap_server)
M.login(imap_user, imap_password)
except Exception, e:
self.list.insert(END, 'IMAP login error: ', e)
return

try:
result, message = M.select(readonly=1)
if result != 'OK':
raise Exception, message
typ, data = M.search(None, '(UNSEEN)')
for num in string.split(data[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.insert(END, text)
len = self.list.size()
if len > 0: self.list.see(len-1)
except Exception, e:
self.list.delete(0,END)
print sys.exc_info()
self.list.insert(END, 'IMAP read error: ', e)
M.logout()
getimapaccount()
root = Tk(className='mailwatcher')
root.title('mailwatcher')
mw = Mailwatcher(root)
mw.getmail()
mw.mainloop()
Mar 23 '06 #1
0 1133

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ian Marlier | last post by:
I'm trying to get a PHP-based webmail setup going, but I'm running into a problem with IMAP authentication. Specifically, the PHP IMAP extension is trying to log in using CRAM-MD5 authentication,...
1
by: Fabian Müller | last post by:
Hello, I would like to access (open) my remote IMAP Mailbox with the imap_open() function in PHP Version 5.0.4. The Mailbox is located at the german provider freenet.de. The imap_open()...
0
by: dekoffie | last post by:
Hello there, I'm trying to create a connection with an IMAP server which is secured through SSL. I can get the connection just fine, and I get a first respone from the server. But then I send...
3
by: chirag | last post by:
i want a IMAP server for windows so that i can retrive incoming mails. I use mssql server to store user information. i need IMAP server to develop project i.e mail system can any body sent me...
0
by: Kevin F | last post by:
Sorry to repost... but 4am didn't get much responses. 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...
2
by: J Huntley Palmer | last post by:
I am having a horrific time integrating uw-imap's c-client for imap support in php. The problem is a whole bunch of "Text relocation remains referenced against symbol" errors during linking....
0
by: contactme | last post by:
Hi, Is it possible to open concurrent connections using Net::IMAP::Simple library ? My IMAP server allows 4 connections per ip, so I am having following problems while using Net::IMAP::Simple and...
4
by: gregpinero | last post by:
I'm trying to get a list of messages from GMAIL using it's new IMAP access. So far I've tried running this command but it just hangs. Any ideas? I figured that's the first line to run from...
11
by: mp- | last post by:
I want to be able to allow people to check their email from my PHP online application. Given only the users 1) email address, 2) username (if applicable) and 3) password - how can I auto detect...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.