Python uses an email module to encode and decode messages

Source: Internet
Author: User
This article introduces to you is Python uses the email module to encode and decode the message, very detailed, with the same needs of small partners can refer to the next





Decode message
Python's own email module is a very interesting thing, it can encode the message decoding, to handle the mail very good.
Processing mail is a very meticulous work, especially decoding the message, because its format changes too much, the following first look at a message source file:





Received: from 192.168.208.56 ( 192.168.208.56 [192.168.208.56] ) by
ajax-webmail-wmsvr37 (Coremail) ; Thu, 12 Apr 2007 12:07:48 +0800 (CST)
Date: Thu, 12 Apr 2007 12:07:48 +0800 (CST)
From: user1 <xxxxxxxx@163.com>
To: zhaowei <zhaoweikid@163.com>
Message-ID: <31571419.200911176350868321.JavaMail.root@bj163app37.163.com>
Subject: =?gbk?B?u+nJtA==?=
MIME-Version: 1.0
Content-Type: multipart/Alternative; 
  boundary="----=_Part_21696_28113972.1176350868319"

------=_Part_21696_28113972.1176350868319
Content-Type: text/plain; charset=gbk
Content-Transfer-Encoding: base64

ztLS0b+qyrzS1M6qysfSu7j20MfG2ru70ru0zqOs1K3AtMrH0ru49tTCtffSu7TOztLDx8/W1NrT
prjDysew67XjssXE3MjI1ebC6bezICAg
------=_Part_21696_28113972.1176350868319
Content-Type: text/html; charset=gbk
Content-Transfer-Encoding: quoted-printable

<p>=CE=D2=D2=D1=BF=AA=CA=BC=D2=D4=CE=AA=CA=C7=D2=BB=B8=F6=D0=C7=C6=DA=BB=
=BB=D2=BB=B4=CE=A3=AC=D4=AD=C0=B4=CA=C7=D2=BB=B8=F6=D4=C2=B5=F7=D2=BB=B4=CE=
</p>
<p>=CE=D2=C3=C7=CF=D6=D4=DA=D3=A6=B8=C3=CA=C7=B0=EB=B5=E3=B2=C5=C4=DC=C8=
=C8</p>
<p>=D5=E6=C2=E9=B7=B3</p>
------=_Part_21696_28113972.1176350868319--





The above is the source file of the message, from the first line to the first empty line between the letter header, followed by the letter body. Copy the above information into a file called xxx.eml, with the mouse to double-click to see the content, of course, see is decoded, is the outlook to help you decode.
Look at how the email module handles this message, assuming the letter has been saved as xxx.eml.





#-*-encoding: gb2312-*-
import email

fp = open ("xxx.eml", "r")
msg = email.message_from_file (fp) # Directly create a message object in the file, this time will also do preliminary decoding
subject = msg.get ("subject") # Take the subject in the letter header, which is the subject
# The following three lines of code are just for decoding subjects like =? Gbk? Q? = CF = E0 = C6 = AC? =
h = email.Header.Header (subject)
dh = email.Header.decode_header (h)
subject = dh [0] [0]
print "subject:", subject
print "from:", email.utils.parseaddr (msg.get ("from")) [1] # take from
print "to:", email.utils.parseaddr (msg.get ("to")) [1] # fetch to

fp.close ()





This code can resolve the subject, sender, and recipient in an email. EMAIL.UTILS.PARSEADDR is used to specifically parse the email address, because the email address is written in the original text: User1 <xxxxxxxx@163.com> EMAIL.UTILS.PARSEADDR can parse it into a list, the first item is User1, the second item is xxxxxxxx@163.com, and here only shows the back with the part.
The previous code just parses the letter header, then parses the letter body. There may be two parts of plain text, plain and HTML, or attachments. Here you need the knowledge of MIME, detailed introduction can be searched from the Internet. I will not say here, see below how to parse:





#-*-encoding: gb2312-*-
import email

fp = open ("xxx.eml", "r")
msg = email.message_from_file (fp)

# Loop through each mime data block in the letter
for par in msg.walk ():
  if not par.is_multipart (): # Here to determine whether it is multipart, if yes, the data inside is useless, as for why you can learn mime related knowledge.
    name = par.get_param ("name") #If it is an attachment, the file name of the attachment will be taken out here
    if name:
      #Has attachment
      # The following three lines of code are just to decode like =? Gbk? Q? = CF = E0 = C6 = AC.rar? = Such a file name
      h = email.Header.Header (name)
      dh = email.Header.decode_header (h)
      fname = dh [0] [0]
      print 'Attachment name:', fname
      data = par.get_payload (decode = True) # Decode the attachment data and store it in a file
      
      try:
        f = open (fname, 'wb') #Note that you must use wb to open the file, because the attachments are generally binary files
      except:
        print 'The attachment name has illegal characters, automatically change one'
        f = open ('aaaa', 'wb')
      f.write (data)
      f.close ()
    else:
      #Not an attachment, but text content
      print par.get_payload (decode = True) # Decode the text content and output it directly.
    
    print '+' * 60 # used to distinguish the output of each part





Simple, and there is not much code can be implemented complex to parse the message function!



Coded messages
Using the email module to generate the email is also very simple, just need some basic knowledge of mime. Let's look at a bit of MIME basics below.
The MIME message consists of two parts: the message header and the message body, and the message header and the message body. A blank line separates the message header from the message body. This can be done by using a text editor (such as Notepad) to see the source files of a message. Outlook and Foxmail have the ability to view the source files themselves.
The message header contains important information such as sender, recipient, subject, Time, MIME version, type of message content, and so on. Each piece of information is called a domain, the domain name after the ":" and the information content, can be a row, longer can occupy more than one line. The first line of the field must be "head", that is, the left cannot have white space characters (spaces and tabs), and the continuation line must begin with a blank character, and the first whitespace character is not intrinsic to the information itself.
The message body contains the contents of the message, and its type is indicated by the "Content-type" field of the message header. The most common types are text/plain (plain text) and text/html (hypertext). The message body is divided into several segments, each of which contains both the segment head and the segment body, and the two parts are separated by a blank line. There are three types of common multipart: multipart/mixed, multipart/related and multipart/alternative. From their names, it is not difficult to infer the respective meanings and uses of these types.
If you want to add an attachment in a message, you must define the multipart/mixed segment, or at least define the multipart/related segment if there is an inline resource, or at least define the multipart/alternative segment if the plain text coexists with the hypertext. Generating the message is about generating the various MIME sections. The email module is packaged for these processes and looks at the build method:





#-*-encoding: gb2312-*-
import email
import string, sys, os, email
import time

class MailCreator:
  def __init __ (self):
    # Create a message object for email
    self.msg = email.Message.Message ()
    self.mail = ""
    
  def create (self, mailheader, maildata, mailattachlist = []):
    # mailheader is a dict type, maildata is a list, and the first item is a plain text type, and the second item is html.
    # mailattachlist is a list, which contains the file name of the attachment
    if not mailheader or not maildata:
      return
    
    for k in mailheader.keys ():
      # Special treatment is required for subject, and Chinese needs to be converted.
      # For example, "One of my test emails" is converted to =? Gb2312? B? ZtK1xNK7uPay4srU08q8 / g ==? =
      if k == 'subject':
        self.msg [k] = email.Header.Header (mailheader [k], 'gb2312')
      else:
        self.msg [k] = mailheader [k]
    # Create plain text section
    body_plain = email.MIMEText.MIMEText (maildata [0], _subtype = 'plain', _charset = 'gb2312')
    body_html = None
    # Create html part, this is optional
    if maildata [1]:
      body_html = email.MIMEText.MIMEText (maildata [1], _subtype = 'html', _charset = 'gb2312')
    
    
    # Create a multipart, and then attach the previous text part and html part to it, as for why, you can take a look at mime related content
    attach = email.MIMEMultipart.MIMEMultipart ()
    attach.attach (body_plain)
    if body_html:
      attach.attach (body_html)
    # Process every attachment
    for fname in mailattachlist:
      attachment = email.MIMEText.MIMEText (email.Encoders._bencode (open (fname, 'rb'). read ()))
      # Set the file type here, all set to Application. Of course it can also be Image, Audio or whatever, no matter how much here
      attachment.replace_header ('Content-type', 'Application / octet-stream; name = "' + os.path.basename (fname) + '"')
      # Be sure to set the transmission encoding to base64, because here is the base64 used by default
      attachment.replace_header ('Content-Transfer-Encoding', 'base64')
      attachment.add_header ('Content-Disposition', 'attachment; filename = "' + os.path.basename (fname) + '"')
      attach.attach (attachment)
    # Generate final email
    self.mail = self.msg.as_string () [:-1] + attach.as_string ()
    
    return self.mail

if __name__ == '__main__':
  mc = MailCreator ()
  header = {'from': 'zhaowei@163.com', 'to': 'weizhao@163.com', 'subject': 'My test email'}
  data = ['plain text information', '<font color = "red"> html text information </ font>']
  if sys.platform == 'win32':
    attach = ['c: /windows/clock.avi']
  else:
    attach = ['/ bin / cp']
  
  mail = mc.create (header, data, attach)
  
  f = open ("test.eml", "wb")
  f.write (mail)
  f.close () 





Here I have encapsulated a class to do the processing, the overall process is:
1. Create the Message object first: email. Message.message ()
2. Create Mimemultipart object: Email.MIMEMultipart.MIMEMultipart ()
3. Create individual Mimetext objects and attach them into Mimemultipart, where the mimetext is not just text, but also image, application, audio and so on.
4. Generate the final message.


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.