[Tutor] Decoding email attahments

Jim Haak HaakJ@masirv.com
Thu, 27 Jun 2002 15:45:26 -0700


Trying to extract attachments from E-mail.  
Appended below are my code and its output.
I can't get email to consistently give me just the attached file.
As shown, if the message is multipart or has a mime version in the header,
then I get the payload.  But if the message is not multipart and has no mime
version, then the payload has some header info which prevents me from
decoding it.  I can't figure out how to get rid of this header info (using
email tools). 
Adding to my confusion, I tried to parse the same messages with mimetools.
But, as shown, the mimetools output for every message is the same and of no
use. 
The 'HAS MIME VERSION' test below is probably not the best way to detect
those nasty X-mailer headers, but it seems to work. Some E-mail with the
X-mailer headers have almost no header information.    
The 'FUNKY PAYLOAD' test doesn't work. I was trying to identify payload that
is actually a file attachment. MSG 4 trapped the funky payload, but MSG 3
failed to do so.

Thanks in advance.

Jim


-----------------  C O D E   ---------------------
import poplib, email, cStringIO, mimetools
inbox = poplib.POP3(mailservername)
inbox.user(username)
inbox.pass_(mypassword)
for message_number in range(1, 5):#len(inbox.list()[1])+1):
    lines = inbox.retr(message_number)[1] 
    fakefile = cStringIO.StringIO( '\n'.join(lines) )  
    message = email.message_from_file(fakefile)
    if message.is_multipart():
        payload = message.get_payload()
        for part in payload:
            part_payload = part.get_payload()
            if part_payload[1] <> '\n':
                print "\nMSG NUMBER", message_number,"IS MULTIPART.
PAYLOAD[:100] OF PART", payload.index(part), "IS:", part_payload[:100]
            else:
                print "\nMSG NUMBER", message_number,"IS MULTIPART.  FUNKY
PAYLOAD[:100] OF PART", payload.index(part), "IS:", part_payload[:100]
    else:
        if ('MIME-Version', '1.0') not in message.items():
            payload = message.get_payload()
            print "\nMSG NUMBER", message_number,"HAS NO MIME VESION.
PAYLOAD[:100] IS:", payload[:100]
            print "\n"
            print type(payload)
        else:
            payload = message.get_payload()
            print "\nMSG NUMBER", message_number,"IS NOT MULTIPART BUT HAS
MIME VESION.  PAYLOAD[:100] IS:", payload[:100]

    msg = mimetools.Message(fakefile)
    print "\nMIMETOOLS:", msg.gettype(), msg.getplist(), msg.getencoding()

-----------------  O U T P U T   ---------------------

MSG NUMBER 1 IS NOT MULTIPART BUT HAS MIME VESION.  PAYLOAD[:100] IS:
UEsDBBQAAAAIANeZqSyQc8wMsAEAAHgJAAAJAAwAQ08wNjUuYTAxVVgIABYt2zwWLds8rZbBbsIw
DIbvSLyDJbQzthM7CadJ2yP

MIMETOOLS: text/plain [] 7bit

MSG NUMBER 2 HAS NO MIME VESION.  PAYLOAD[:100] IS: See attached file





begin 600 AA066.ZIP
M4$L#!!0````(`'V<J2P%"32V;0(``"T0```)``P`04$P-C8N83`Q55@(


<type 'str'>

MIMETOOLS: text/plain [] 7bit

MSG NUMBER 3 IS MULTIPART.  PAYLOAD[:100] OF PART 0 IS:   
 
 9V-SMV.ZIP



MSG NUMBER 3 IS MULTIPART.  PAYLOAD[:100] OF PART 1 IS:
UEsDBBQAAAAIAHkmqiwcT6ZaqgkAAKZEAAAKAAwAOVYtU01WLmEwMVVYCABWtNs8VrTbPL1cy47l
thHdG/A/CDC8NossvmYVIPm

MIMETOOLS: text/plain [] 7bit

MSG NUMBER 4 IS MULTIPART.  FUNKY PAYLOAD[:100] OF PART 0 IS:  



MSG NUMBER 4 IS MULTIPART.  PAYLOAD[:100] OF PART 1 IS:
UEsDBBQAAAAIAJ00qixzU4bXUQMAANsYAAAJAAwAQ08wNjUuYTAxVVgIAPrM2zz6zNs8rZjNihsx
DIDvgbyDYel5LVmS7ZwK7SP

MIMETOOLS: text/plain [] 7bit