imaplib -- can't read body

Sells, Fred fred.sells at adventistcare.org
Fri Jul 11 20:20:36 CEST 2008


I'm trying to read mail using the imaplib module.  I can get the subject and date, but not the body, have not found any example on how to do that and I don't know much about imap.  Here's what I have, working as noted...

If anyone can show me what I'm missing in order to get the body of a mail message, that would be greatly appreciated,

Fred
------------------- code starts below-------------------------------

import imaplib, sys, os, re, rfc822

OK = "OK"

FETCHTHIS = '(BODY[HEADER.FIELDS (SUBJECT FROM)])'

### got inemsg from the web, works up to the last line; msg.fp has no read and readline returns empty string.
class inemsg:
    def __init__(self,msg):
        (self.msgfromname, self.msgfrom) = msg.getaddr('from')
        self.msgto = msg.getaddr('to')
        self.msgsubject = msg.getheader('subject')
        self.msgdate = msg.getheader('date')
        self.msgtext = msg.fp.read()
        

class msg: # a file-like object for passing a string to rfc822.Message
    def __init__(self, text):
        self.lines = text.split('\015\012')
        self.lines.reverse()
    def readline(self):
        try: return self.lines.pop() + '\n'
        except: return ''
        
    
class MailReader:
    def __init__(self, mailserver, user, password):
        self.M = imaplib.IMAP4(mailserver)
        self.M.login(user, password)
        result, message = self.M.select(readonly=1)
        print ( 'constructor', result, message)
        if result != OK: raise Exception, message
        
    def getMessages(self, *mailboxes):
        if mailboxes:
            filter = '(%s)' % ' '.join(list(mailboxes))
        else:
            filter = '(UNSEEN UNDELETED)'
        mtype, data = self.M.search(None, filter )
        print ('getMessages', mtype, data)
        for num in data[0].split():
            f = self.M.fetch(num, FETCHTHIS)
            print ('fetch', f)
            m = rfc822.Message(msg(f[1][0][1]), 0)
            subject = m['subject']
            fromaddr = m.getaddr('from')
            if fromaddr[0]=='': n = fromaddr[1]
            else: n=fromaddr[0]
            print (subject, fromaddr) 
	return None   #no reason to return until I can get the message content/body.   
            
            
if __name__=='__main__':
    x = MailReader("myserver", "myuser", "mypassword")
    messages = x.getMessages()
    print messages

---------------------------------------------------------------------------
The information contained in this message may be privileged and / or
confidential and protected from disclosure. If the reader of this message is
not the intended recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited. If you
have received this communication in error, please notify the sender
immediately by replying to this message and deleting the material from any
computer.
---------------------------------------------------------------------------



More information about the Python-list mailing list