,           ,
 /             \ 
((__-^^-,-^^-__))
 `-_---' `---_-'
  `--|o` 'o|--'     TWiki . Codedev
     \  `  /
      ): :(
      :o_o:
       "-"
::: PythonTidbits :::
Search: 
  TWiki . Codedev . PythonTidbits # Edit # Attach # Diffs # Printable # More :::
Logged in as TWikiGuest

OSIMS

Codedev
WebChanges
WebIndex
WebNotify
WebStatistics

People

TWiki System

Sandbox


Python Tidbits


PythonTextToSpeech


tray icon app wxPython

http://mail.python.org/pipermail/python-list/2003-January/141014.html

http://radio.weblogs.com/0110159/gems/systray_py.txt


wxWindows and twisted event loop:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/181780


check mail:

#adapted from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52299
#thanks Brent Burley

import imaplib,string,getpass,rfc822,os,re,sys
class msg:
    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:
    def getmail(self):
        answer={}
        self.passwd="<PASSWORD>"
        try:
            M=imaplib.IMAP4()
            M.login(getpass.getuser(),self.passwd)
        except Exception, e:
            print "Imap error %s" % e
            return
        try:
            result,message=M.select(readonly=1)
            if result !='OK':
                raise Exception, message
            typ, data=M.uid('search',None,'(UNSEEN UNDELETED)')
            for num in string.split(data[0]):
                try:
                    f=M.uid('fetch',num,'(UID BODY[HEADER.FIELDS (SUBJECT FROM)])')
                    m=rfc822.Message(msg(f[1][0][1]),0)
                    subject=m['subject']
                except KeyError:
                    f=M.uid('fetch',num,'(UID 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)
                msgline= "UID: %s"% num + " from: %s" %n + " Subject: %s " %subject
                if answer.has_key(num):
                    pass
                else:
                    answer[num]=msgline
        except:
            pass
        M.logout()
        return answer

mw=Mailwatcher()
list=mw.getmail()
for num in list:
   print list[num]

Uses:

ManiacBot can use it via JabberMessaging to notify of new mail arrival. Part of the PersonalBotProject

# Edit menu  


Revision r1.3 - 01 Jun 2003 - 21:45 GMT - DarrylVanDorp

Copyright © 2000 by the contributing authors. All material on this collaboration tool is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.

SourceForge