Python Common Module Email----Extract header information from the original message

Source: Internet
Author: User

email.parser.Parser()的parsestr()和parse()方法都可以用来从原始邮件文件中提取邮件头信息。这两个方法的区别就parsestr处理的是字符串对象,parse处理的是文件对象。

  让我们通过两个例子来了解这两个方法的使用。先来看一封邮件的原始信息的一部分:

cat/tmp/email_test1.txtdate:wed, 16:04:44 +0800from:3456789 <[email protected]>to:1234567 <[email protected]>cc:23456780 <[email protected]>reply-to:3456789 <[email protected]>subject:email test! X-priority:3x-has-attach:nox-mailer:foxmail 7.0.1.91[cn]mime-version:1.0message-id: <[email protected]> content-type:multipart/alternative;boundary= "----=_001_nextpart245273401224_=----"


Example 1: Extracting headers from PARSESTR

#!/usr/bin/pythonimport osfrom email.parser import parserdef read_mail (PATH):                          if os.path.exists (PATH):                     with open (path) &NBSP;AS&NBSP;FP:              email=fp.read ()              return email             else:                                         print  "file not exist!"       &Nbsp;         raw_email=read_mail ('/tmp/email_test1.txt ')     #将邮件读到一个字符串里面headers  =parser () parsestr (raw_email) #经过parsestr处理过后生成一个字典print   ' cc: %s '  % headers[' Cc ']print  ' to: %s '  % headers[' to ']print  ' From: %s '  %  headers[' from ']print  ' subject: %s '  % headers[' Subject ']


Example 2: Extracting a message by using parse

#!/usr/bin/pythonfrom email.parser Import parserheaders = Parser (). Parse (open ('/tmp/email_test1.txt ', ' R ')) print ' Cc: %s '% headers[' Cc ']print ' to:%s '% headers[' to ']print ' from:%s '% headers[' from ']print ' Subject:%s '% headers[' Subject ' ]


The results of the return of the two examples are the same:

cc:23456780 <[email protected]>to:1234567 <[email protected]>from:3456789 <[email protected]> Subject:email test!


So we have finished the message header information extraction!

This article is from the "Always on the Road" blog, please be sure to keep this source http://chenql.blog.51cto.com/8732050/1873770

Python Common Module Email----Extract header information from the original 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.