python常用模块email----从原始邮件中提取邮件头信息

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

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

cat /tmp/email_test1.txt
date: Wed, 16 Nov 2016 16:04:44 +0800
From: 3456789 <[email protected]>
To: 1234567 <[email protected]>
Cc: 23456780 <[email protected]>
Reply-To: 3456789 <[email protected]>
Subject: email test!
X-Priority: 3
X-Has-Attach: no
X-Mailer: Foxmail 7.0.1.91[cn]
Mime-Version: 1.0
Message-ID: <[email protected]>
Content-Type: multipart/alternative;
	boundary="----=_001_NextPart245273401224_=----"

例1:通过parsestr提取邮件头

#!/usr/bin/python
import os
from email.parser import Parser

def read_mail(path):                    
    if os.path.exists(path):            
        with open(path) as fp: 
            email=fp.read()
            return email        
    else:                               
        print "file not exist!"                

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‘]

例2:通过parse提取邮件关

#!/usr/bin/python

from email.parser import Parser
headers = 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‘]

两个例子的返回的结果都是一样的:

Cc: 23456780 <[email protected]>
To: 1234567 <[email protected]>
From: 3456789 <[email protected]>
Subject: email test!

这样我们就完成了邮件头部信息的提取!

时间: 2024-08-14 22:03:38

python常用模块email----从原始邮件中提取邮件头信息的相关文章

实战篇一 python常用模块和库介绍

# [email protected] coding: utf-8 [email protected] -- Python 常用模块和库介绍 第一部分:json模块介绍 import json 将一个Python数据结构转换为JSON: dict_ = {1:2, 3:4, "55":"66"} # test json.dumps print type(dict_), dict_ json_str = json.dumps(dict_) print "js

python——常用模块

time.asctime(time.localtime(1234324422)) python--常用模块 1 什么是模块: 模块就是py文件 2 import time #导入时间模块 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: (1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行"type(time.time())",返回的是float类型.

Python常用模块-随机数模块(random)

Python常用模块-随机数模块(random) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常用方法举例 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7

Python常用模块-摘要算法(hashlib)

Python常用模块-摘要算法(hashlib) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MD5算法参数详解 1.十六进制md5算法摘要 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%

Python常用模块——random随机模块

Python常用模块--random随机模块 程序中有很多地方都需要用到随机字符,比如登录网站的随机验证码,通过random模块可以很容易生成随机字符串. >>> random.randrange(1,10) #返回1-10之间的一个随机数,不包括10 >>> random.randint(1,10) #返回1-10之间的一个随机数,包括10 >>> random.randrange(0, 100, 2) #随机选取0到100间的偶数,即步长为2 &g

Python常用模块——正则表达式re模块

Python常用模块--正则表达式re模块 引子 请从以下文件里取出所有的手机号 姓名 地区 身高 体重 电话 况咏蜜 北京 171 48 13651054608 王心颜 上海 169 46 13813234424 马纤羽 深圳 173 50 13744234523 乔亦菲 广州 172 52 15823423525 罗梦竹 北京 175 49 18623423421 刘诺涵 北京 170 48 18623423765 岳妮妮 深圳 177 54 18835324553 贺婉萱 深圳 174 5

python——常用模块2

python--常用模块2 1 logging模块 1.1 函数式简单配置 import logging logging.debug("debug message") logging.info("info message") logging.warning("warning message") logging.error("error message") logging.critical('critical message')

python 常用模块 time random os模块 sys模块 json &amp; pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess logging re正则 转自老男孩老师Yuan:http://www.cnblogs.com/yuanchenqi/articles/5732581.html 模块&包(* * * * *) 模块(modue)的概念: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,

python常用模块之time&amp;datetime模块

python常用模块之time&datetime模块 在平常的代码中,我们经常要与时间打交道.在python中,与时间处理有关的模块就包括:time和datetime,下面分别来介绍: 在开始之前,首先要说明有以下几种方式来表示时间: 1.时间戳 2.格式化的时间字符串(时间对象) 3.元组(struct_time)共九个元素,由于python的time模块实现主要调用C库,所以各个平台可能不同 几个定义 UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文