views:

233

answers:

1

I am using python to open an email on the server (POP3). Each email has an attachment which is a forwarded email itself.

I need to get the "To:" address out of the attachment.

I am using python to try and help me learn the language and I'm not that good yet !

The code I have already is this

import poplib, email, mimetypes

    oPop = poplib.POP3( 'xx.xxx.xx.xx' )
    oPop.user( '[email protected]' )
    oPop.pass_( 'xxxxxx' )

    (iNumMessages, iTotalSize ) = oPop.stat()

    for thisNum in range(1, iNumMessages + 1): 
          (server_msg, body, octets) = oPop.retr(thisNum)
          sMail = "\n".join( body )

          oMsg = email.message_from_string( sMail )

          # now what ??

I understand that I have the email as an instance of the email class but I'm not sure how to get to the attachment

I know that using

  sData = 'To'
       if sData in oMsg:
    print sData + "", oMsg[sData]

gets me the 'To:' header from the main message but how do I get that from the attachment ?

I've tried

for part in oMsg.walk():
 oAttach = part.get_payload(1)

But I'm not sure what to do with the oAttach object. I tried turning it into a string and then passing it to

oMsgAttach = email.message_from_string( oAttach )

But that does nothing. I'm a little overwhelmed by the python docs and need some help. Thanks in advance.

+1  A: 

Without having an email in my inbox that is representative, it's difficult to work this one through (I've never used poplib). Having said that, some things that might help from my little bit of investigation:

First of all, make lots of use of the command line interface to python and the dir() and help() functions: these can tell you lots about what's coming out. You can always insert help(oAttach), dir(oAttach) and print oAttach in your code to get an idea of what's going on as it loops round. If you're typing it into the command line interface line-by-line, it's even easier in this case.

What I think you need to do is to go through each attachment and work out what it is. For a conventional email attachment, it's probably base64 encoded, so something like this might help:

#!/usr/bin/python
import poplib, email, mimetypes

# Do everything you've done in the first code block of your question
# ...
# ...

import base64
for part in oMsg.walk():
    # I've removed the '1' from the argument as I think you always get the
    # the first entry (in my test, it was the third iteration that did it).
    # However, I could be wrong...
    oAttach = part.get_payload()
    # Decode the base64 encoded attachment
    oContent = b64decode(oAttach)
    # then maybe...?
    oMsgAttach = email.message_from_string(oContent)

Note that you probably need to check oAttach in each case to check that it looks like a message. When you've got your sMail variable, print it out to the screen. Then you can look for something like Content-Transfer-Encoding: base64 in there, which will give you a clue to how the attachment is encoded.

As I said, I've not used any of the poplib, email or mimetypes modules, so I'm not sure whether that'll help, but I thought it might point you in the right direction.

Al
Thanks for this, it helped. I've sorted it out now. I'll post the solution to my blog and add it back here shortly
neilc