email - How to receive mail using python -
i receive email using python. far have been able subject not body. here code have been using:
import poplib email import parser pop_conn = poplib.pop3_ssl('pop.gmail.com') pop_conn.user('myusername') pop_conn.pass_('mypassword') #get messages server: messages = [pop_conn.retr(i) in range(1, len(pop_conn.list()[1]) + 1)] # concat message pieces: messages = ["\n".join(mssg[1]) mssg in messages] #parse message intom email object: messages = [parser.parser().parsestr(mssg) mssg in messages] message in messages: print message['subject'] print message['body'] pop_conn.quit()
my issue when run code returns subject not body. if send email subject "tester" , body "this test message" looks in idle.
>>>>tester >>>>none
so appears accurately assessing subject not body, think in parsing method right? issue don't know enough these libraries figure out how change returns both subject , body.
the object message not have body, need parse multiple parts, this:
for part in message.walk(): if part.get_content_type(): body = part.get_payload(decode=true)
the walk()
function iterates depth-first through parts of email, , looking parts have content-type. content types can either text/plain
or text/html
, , 1 e-mail can contain both (if message content_type
set multipart/alternative
).
Comments
Post a Comment