How Python reads the message data and downloads An example of an attachment

Source: Internet
Author: User
This article mainly describes the Python read the message data and download an example of the attachment of the relevant information, here to provide an example of implementation, to help you learn to understand this part of the content, the need for friends can refer to the following

Detailed Python implementation read the message data and download an instance of the attachment

To achieve a result diagram:

Implementation code:


#!/usr/bin/python2.7# _*_ coding:utf-8 _*_ "" "@Author: Markliu" "" Import poplibimport emailfrom email.parser Import Parserfrom email.header Import decode_headerfrom email.utils import parseaddrdef decode_str (s): value, CharSet = Decode_h Eader (s) [0] If Charset:value = Value.decode (charset) return valuedef Guess_charset (msg): # First get the encoding from the MSG object: CharSet = Msg.get_charset () If CharSet is None: # if not obtained, then obtained from the Content-type field: Content_Type = Msg.get (' Content-type ', '). Lower () pos = Content_type.find (' charset= ') if pos >= 0:charset = Content_type[pos + 8:].strip () return charset def get_email_headers (msg): # Message from, to, Subject exists on the root object: headers = {} for header in [' From ', ' to ', ' Subject ', ' Date '] : value = Msg.get (header, ') if value:if header = = ' Date ': headers[' date ' = value if header = = '        Subject ': # needs to decode Subject string: Subject = decode_str (value) headers[' Subject '] = Subject else: # need to decode email address: HDR, ADDr = Parseaddr (value) name = Decode_str (HDR) value = U '%s <%s> '% (name, addr) If header = = ' Fr          Om ': from_address = value headers[' from '] = from_address else:to_address = value headers[' to ' = to_address Content_Type = Msg.get_content_type () print ' head content_type: ', Content_Type return he aders# indent for indentation display: def get_email_cntent (Message, Base_save_path): j = 0 content = "Attachment_files = [] for Part I  n Message.walk (): j = j + 1 file_name = Part.get_filename () ContentType = Part.get_content_type () # Save Attachment If file_name: # Attachment # Decode filename h = email. Header.header (file_name) dh = email. Header.decode_header (h) filename = dh[0][0] If dh[0][1]: # If the encoded format is included, the format is decoded by the filename = Unicode (filename , dh[0][1]) filename = Filename.encode ("utf-8") data = Part.get_payload (decode=true) att_file = open (base _save_path + filename, ' WB ') AttachmeNt_files.append (filename) att_file.write (data) att_file.close () elif ContentType = = ' Text/plain ' or Contentty PE = = ' text/html ': # Save Body data = Part.get_payload (decode=true) charset = Guess_charset (part) if Charse T:charset = Charset.strip (). Split (';') [0] print ' charset: ', CharSet data = Data.decode (charset) content = Data return content, Attachment_fi  Lesif __name__ = = ' __main__ ': # Enter e-mail address, password and POP3 server address: EmailAddress = ' xxxxxx@163.com ' # Note the authorization code using the open pop,smtp and so on password = ' xxxxxx ' pop3_server = ' pop.163.com ' # connection to POP3 server: Server = poplib. POP3 (pop3_server) # can turn debug info on or off: # server.set_debuglevel (1) # POP3 Welcome text for server: Print Server.getwelcome () # Authentication: Server . User (EmailAddress) server.pass_ (password) # stat () returns the number of messages and space occupied: messagescount, messagessize = Server.stat () print ' Mes  Sagescount: ', messagescount print ' messagessize: ', Messagessize # list () returns the number of all messages: resp, mails, octets = Server.list ()  print '------resp------'Print resp # +ok 46 964346 Responses to status message quantity messages Occupy space size print '------mails------' Print mails # number and size of all messages list,[' 1 2211 ', '  2 29908 ', ...] print '------octets------' Print octets # Get the latest message, note that the index number starts at 1: length = len (mails) for I in range (length): RESP, Lines, octets = SERVER.RETR (i + 1) # lines stores each line of the original text of the message, # can get the original text of the entire message: Msg_content = ' \ n '. Join (lines) # Mail  The piece content resolves to a message object: Msg = Parser (). PARSESTR (msg_content) # But the message object itself could be a Mimemultipart object that contains nested other Mimebase objects, # Nesting may be more than one level. So we're going to print out the hierarchy of the Message object recursively: print '----------after parsing----------' Base_save_path = '/media/markliu/entertainment/email_a    ttachments/' msg_headers = get_email_headers (msg) content, Attachment_files = Get_email_cntent (msg, Base_save_path) print ' Subject: ', msg_headers[' subject ' "Print ' from_address: ', msg_headers[' from '] print ' to_address: ', Msg_head ers[' to ' print ' Date: ', msg_headers[' date ' print ' content: ', content print ' Attachment_files: ', Attachment_Files # Close connection: Server.quit () 
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.