Extracting TIFF from emails

Ryan Swift ryan at ryanswift.com
Thu Sep 4 11:15:28 EDT 2003


Apparently I am getting the email as a list, but I don't know of a way
to do it otherwise.  I have read through the docs on www.python.org
for the email module, but there is not much on reading mail, rather it
focuses more on creating it.  Below is my *complete* code (I am fairly
sure I am importing more than I need to), is there a way to get a
message from the server in a form other than a list?  I assume this
can be done with the email module but I must be too dim to find it. 
Can you provide a link to information about the standard library email
module?  Once again, thanks.

import poplib, os, mailconfig, email, mimetools

mailserver = mailconfig.popservername    
mailuser   = mailconfig.popusername
mailpasswd = mailconfig.poppassword
dir = os.curdir

def connect(mailserver, mailuser, mailpasswd):
    print '\nConnecting...'
    server = poplib.POP3(mailserver)
    server.user(mailuser)
    server.pass_(mailpasswd)
    return server


if __name__ == "__main__":
    server = connect(mailserver, mailuser, mailpasswd)
    try:
        (msgCount, msgBytes) = server.stat()
        print '\nThere are', msgCount, 'mail messages, total',
msgBytes, 'bytes'
        print 'Retrieving message', msgCount, '\n'
        (hdr, msg, octets) = server.retr(msgCount)
        email_file = open('email.txt', 'w')
        email_file.write(message)
        email_file.close()
    finally:
        server.quit()
    print 'Closed connection.'

    fp = open('email.txt')
    filemsg = email.message_from_file(fp)
    fp.close()

    for part in filemsg.walk:
            print part.get_content_type()




More information about the Python-list mailing list