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.