Python's Poplib module downloads and resolves messages

Source: Internet
Author: User


#-*-coding: utf-8-*-
#python 27
#xiaodeng
#python 之 poplibmodule download and parse mail
#https: //github.com/michaelliao/learn-python/blob/master/email/fetchmail_pop3.py
import poplib, email
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr


#Coding processing
def guess_charset (msg):
    charset = msg.get_charset () # Get encoding from msg object
    if charset is None:
        content_type = msg.get (‘Content-Type’, ‘‘) .lower () #
        if ‘charset’ in content_type:
            charset = content_type.split (‘charset =‘) [1] .strip ()
            return charset
    return charset


# 数据 decode
def decode_str (s):
    value, charset = decode_header (s) [0] #data, data encoding method, from email.header import decode_header
    if charset:
        value = value.decode (charset)
    return value

#print_ingoFunction:
def print_info (msg, indent = 0): #indent for indent display
    if indent == 0:
        for header in [‘From’, ‘To’, ‘Subject’]: #From, to, and subject of the mail exist on the root object
            value = msg.get (header, ‘‘)
            if value:
                if header == ‘Subject’:
                    value = decode_str (value) # Need to decode subject string
                else:
                    #Decode mail address
                    hdr, addr = parseaddr (value)
                    name = decode_str (hdr)
                    value = ‘% s’% (addr)
            print ‘% s:% s’% (header, value)
            print ‘-‘ * 20
    if (msg.is_multipart ()):
        #If the mail object is an is_multipart, get_payload () returns a list containing all child objects
        parts = msg.get_payload () # loop to get list items
        for n, part in enumerate (parts):
            #print (‘% spart% s’% (‘‘ * indent, n))
            #print (‘% s ------------‘% (‘‘ * indent))
            #Recursively print without a child object
            print_info (part, indent + 1)
    else:
        #Mail object is not an is_multipart, just judge based on content_type
        content_type = msg.get_content_type () # Data type
        if content_type == ‘text / plain’ or content_type == ‘text / html’: # plain text html text
            #Plain text or html content
            content = msg.get_payload (decode = True) #Get the string of the text object instead of the object itself
            charset = guess_charset (msg) #To detect text encoding
            if charset: content = content.decode (charset)
            content = ‘% s’% (content)
            print content # Get the text content of the email. If there is only text, the print result will be exactly the same as that seen in the email
        else:
            print ‘not text’
            

#Link to pop3 server
server = poplib.POP3 (‘pop.163.com’)

#Print pop3 server welcome object
# server.getwelcome ()


#Authentication
email = ‘[email protected]’
password = ‘xxxx’
server.user (email) #Enter the email address
server.pass_ (password) #Enter password
#print (‘Messages:% s. Size:% s’% server.stat ()) # Messages: 3. Size: 36090


#Request message list, return the number of all messages; you can view the returned list similar to [‘1 82923’, ‘2 2184’, ...]
resp, mails, octets = server.list ()


#Get the latest email
#Poplib module's retr () function is used to download mail. Every time it happens to download a message, we have to pass the number of messages he wants to download.
#print mails # [‘1 2721’, ‘2 2784’, ‘3 2986’, ‘4 28987’, ‘5 10056’, ‘6 753’, ‘7 763’]
#Note that the index number starts from 1, then the latest email is the one with the largest index
lenString = len (mails)
resp, mailContent, octets = server.retr (lenString) #mailContent: mail content


#Parse mail: only need one line of code to parse mail content into Message object
msg = Parser (). parsestr (‘\ r \ n‘.join (mailContent))


#Print the message content, call the print_info function:
print_info (msg)


# server.dele (len (mails)) # Caution: Mail will be deleted directly from the server:


#Close the connection:
server.quit ()


‘‘ ‘
From: [email protected]
----------------------------------------
To: [email protected]
----------------------------------------
Subject: I am the title
----------------------------------------
Seeing me means the email was sent successfully
‘‘ ‘ 





Python's Poplib module downloads and resolves messages


Related Article

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.