How to receive mail using python

I want to receive email using python. So far I have managed to get an item, but not a body. Here is the code I used:

import poplib from email import parser pop_conn = poplib.POP3_SSL('pop.gmail.com') pop_conn.user('myusername') pop_conn.pass_('mypassword') #Get messages from server: messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)] # Concat message pieces: messages = ["\n".join(mssg[1]) for mssg in messages] #Parse message intom an email object: messages = [parser.Parser().parsestr(mssg) for mssg in messages] for message in messages: print message['subject'] print message['body'] pop_conn.quit() 

My problem is that when I run this code, it returns the theme correctly, but not the body. Therefore, if I send an email with the subject โ€œTesterโ€ and the body โ€œThis is a test messageโ€, it looks like IDLE.

 >>>>Tester >>>>None 

So, this seems to be accurately evaluating the subject, but not the body, I think this is correct in the parsing method? The problem is that I do not know enough about these libraries to figure out how to change it so that it returns both an object and a body.

+6
python email
source share
4 answers

The message of the object does not have a body, you will need to parse several parts, for example:

 for part in message.walk(): if part.get_content_type(): body = part.get_payload(decode=True) 

The walk() function iterates in depth first in parts of the email, and you are looking for parts that have a content type. Content types can be either text/plain or text/html , and sometimes one message can contain both (if the content_type message is set to multipart/alternative ).

+8
source share

The email parser returns an email.message.Message object that does not contain the body key, as you will see if you run

 print message.keys() 

You want to use the get_payload() method:

 for message in messages: print message['subject'] print message.get_payload() pop_conn.quit() 

But this is complicated when it comes to multi-part messages; get_payload() returns a list of parts, each of which is a Message object. You can get a specific part of a multi-page message using get_payload(i) , which returns the i th part, raises IndexError if i is out of range, or raises TypeError if the message is not multi-part.

As Gustavo Costa De Oliveir points out, you can use the walk() method to organize details - it performs a walk around the depth of parts and substrings of a message.

Read more about the email.parser module at http://docs.python.org/library/email.message.html#email.message.Message .

+5
source share

it is also good returned data in the correct encoding in the message contain some multilingual content

 charset = part.get_content_charset() content = part.get_payload(decode=True) content = content.decode(charset).encode('utf-8')
charset = part.get_content_charset() content = part.get_payload(decode=True) content = content.decode(charset).encode('utf-8') 
+2
source share

If you want to use IMAP4. Use the python Outlook library, download here: https://github.com/awangga/outlook to receive an unread message from your mailbox:

 import outlook mail = outlook.Outlook() mail.login(' emailaccount@live.com ','yourpassword') mail.inbox() print mail.unread() 

To retrieve an email item:

 print mail.mailbody() print mail.mailsubject() print mail.mailfrom() print mail.mailto() 
0
source share

All Articles