How to use the poplib module to receive emails in Python

Source: Internet
Author: User
This article mainly introduces how to use the poplib module to receive emails in Python. the code is based on Python2.x. if you need it, refer to SMTP for sending emails. what if you want to receive emails?

Receiving an email is to write a MUA as the client and obtain the email from the MDA to the user's computer or mobile phone. The most common protocol for receiving emails is the POP protocol. The current version number is 3, commonly known as POP3.

Python has a built-in poplib module that implements the POP3 protocol and can be directly used to receive emails.

Note that the POP3 protocol collects not a readable email, but the original text of the email, which is similar to the SMTP protocol. SMTP also sends a large part of the encoded text.

To convert the texts received by POP3 into readable emails, you also need to use the various types provided by the email module to parse the original texts and convert them into readable email objects.

Therefore, the email is received in two steps:

Step 1: Use poplib to download the original text of the email to your local device;

Part 2: use email to parse the original text and restore it to the email object.
Download emails through POP3

The POP3 protocol is simple. the following code is used as an example to obtain the latest email content:

Import poplib # enter the email address, password, and POP3 server address: Email = raw_input ('email: ') Password = raw_input ('password:') pop3_server = raw_input ('pop3 server: ') # connect to the POP3 server: server = poplib. POP3 (pop3_server) # debug information can be turned on or off: # server. set_debuglevel (1) # Optional: print the welcome text of the POP3 server: print (server. getwelcome () # authentication: server. user (email) server. pass _ (password) # stat () returns the number of mails and occupied space: print ('messages: % s. size: % s' % server. stat () # list () returns the number of all emails: resp, mails, octets = server. list () # You can view the returned list, which is similar to ['1 100', '2 100',...] print (mails) # Get the latest email. Note that the index number starts from 1: index = len (mails) resp, lines, octets = server. retr (index) # lines stores each line of the original text of the mail. # you can obtain the original text of the entire mail: msg_content = '\ r \ n '. join (lines) # resolve the email later: msg = Parser (). parsestr (msg_content) # you can delete emails directly from the server based on the mail index number: # server. dele (index) # close the connection: server. quit ()

It is actually very easy to get emails using POP3. to get all emails, you only need to use retr () repeatedly to get the content of each email. What is really troublesome is to resolve the original content of the email to an email object that can be read.
Email resolution

The process of email resolution is the opposite of that of constructing the email in the previous section. Therefore, pilot the necessary modules:

import emailfrom email.parser import Parserfrom email.header import decode_headerfrom email.utils import parseaddr

You only need a line of code to parse the Mail content into a Message object:

msg = Parser().parsestr(msg_content)

However, the Message object itself may be a MIMEMultipart object, that is, other MIMEBase objects containing nesting, and there may be more than one layer of nesting.

Therefore, we need to print the Message object hierarchy recursively:

# Indent: def print_info (msg, indent = 0): if indent = 0: # From, To, and Subject of the email exist on the root object: for header in ['from', 'to', 'subobject']: value = msg. get (header, '') if value: if header = 'subobject': # you need to decode the Subject string: value = decode_str (value) else: # you need to decode the Email address: hdr, addr = parseaddr (value) name = decode_str (hdr) value = u '% s <% s>' % (name, addr) print ('% s: % s' % (''* indent, header, value) if (msg. is_multipart (): # if the email object is a MIMEMultipart, # get_payload () returns a list containing all sub-objects: parts = msg. get_payload () for n, part in enumerate (parts): print ('% spart % s' % (''* indent, n )) print ('% s ------------------' % (''* indent) # print each sub-object recursively: print_info (part, indent + 1) else: # The email object is not a MIMEMultipart, # content_type: content_type = msg. get_content_type () if content_type = 'text/plain 'or content_type = 'text/html': # plain text or html content: content = msg. get_payload (decode = True) # to detect the Text Encoding: charset = guess_charset (msg) if charset: content = content. decode (charset) print ('% sText: % s' % (''* indent, content + '... ') else: # It is not a text and processed as an attachment: print (' % sAttachment: % s' % (''* indent, content_type ))

The Subject or Email of an Email contains the encoded str. to be properly displayed, decode is required:

def decode_str(s):  value, charset = decode_header(s)[0]  if charset:    value = value.decode(charset)  return value

Decode_header () returns a list. because fields such as Cc and Bcc may contain multiple email addresses, multiple elements are parsed. In the above code, we stole a lazy and only took the first element.

Text mail content is also str, also need to detect the encoding, otherwise, non-UTF-8 encoding of the Mail can not be properly displayed:

Def guess_charset (msg): # first obtain the code from the msg object: charset = msg. get_charset () if charset is None: # if not, retrieve content_type = msg from the Content-Type field. get ('content-type ',''). lower () pos = content_type.find ('charset = ') if pos> = 0: charset = content_type [pos + 8:]. strip () return charset

After finishing the above code, we can try to receive an email. First, send an email to your mailbox, and then log on to the mailbox in the browser to see if the email has been received. if so, we will use the Python program to receive it locally:

Run the program. The result is as follows:

+ OK Welcome to coremail Mail Pop3 Server (163 coms [...]) Messages: 126. Size: 27228317 From: Test
 
  
To: Python enthusiasts
  
   
Subject: use POP3 to receive mails part 0 -------------------- part 0 -------------------- Text: Python can use POP3 to receive mails ......... Part 1 -------------------- Text: Python can use POP3 to receive emails ......... Part 1 -------------------- Attachment: application/octet-stream
  
 

We can see from the printed structure that this email is a MIMEMultipart, which contains two parts: the first part is a MIMEMultipart, and the second part is an attachment. The embedded MIMEMultipart is an alternative type, which contains a text-only MIMEText and an HTML MIMEText.
Summary

The Python poplib module collects emails in two steps: the first step is to use the POP3 protocol to obtain the emails locally, and the second step is to use the email module to parse the original emails as Message objects. then, display the email content to the user in an appropriate way.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.