Python email

L

LutherRevisited

I'm attempting to write an email client, and I've run into a snag. I've seen
several docs on email, but they're not dumbed down enough for me. Basically
I'm downloading my messages like this:
M = poplib.POP3('pop.mail.yahoo.com')
M.user('username')
M.pass_('password')
inMail = str(M.retr(i))
and I get the message just fine, but I want to pull out of all that just the
html part. How can I do this.
 
J

Jeffrey Froman

LutherRevisited said:
I get the message just fine, but I want to pull out of all that just the
html part.  How can I do this.

The email and email.Iterators modules offer methods for doing this. For
example:

***************************************************************
# Warning: untested code
import email
from email.Iterators import typed_subpart_iterator

inMail = derived_from_POP3_magic()
my_email = email.message_as_string(inMail)
html_parts = typed_subpart_iterator(my_email, 'text', 'html')

***************************************************************
You may also want to look at email.Iterators.body_line_iterator, useful for
bypassing the headers in each part of the email.

Jeffrey
 
L

LutherRevisited

I have no idea what this derived from POP3Magic thing is, can you give me more
on that. I've begun to familiarize myself with the methods in email, but my
big snag seems to be that I get a single string that I can't manipulate with
these methods from what should be a multi-part message.
 
L

LutherRevisited

I still want to know about that POP3Magic thing, but I finally got this
working. I couldn't figure out why until just now. First I made sure I
retrieved the message as a list, then I iterated through it and concantenated
all that together into a string. Then I cast that as an email object. I
couldn't get the subsomething iterator thing to work, but I was able to walk
through it and check each part for content type. If anyone's interested here's
my code I used to test to see that this actually worked:
for x in M.retr(i)[1]:
stringMail += x
stringMail += '\n'
inMail = email.message_from_string(stringMail)
for part in inMail.walk():
if part.get_content_type() == 'text/html':
print part
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,684
Latest member
sparada

Latest Threads

Top