专栏首页Hongtenpython开发_email_读取邮件头信息

python开发_email_读取邮件头信息

python中的类库中,python自带了email模块。

email模块中,我们可以email类库实现对邮件的读取,和邮件的发送等功能。

本文先来谈谈在python中,利用email模块读取邮件头.....

先来看看一封百度云发给我的邮件:

下面是邮件的具体信息:

Date: Tue, 30 Jul 2013 16:53:17 +0800
Received: from db-sysnoc-mailrelay3.db01.baidu (unknown [123.125.66.194])
    by newmx38.qq.com (NewMx) with SMTP id 
    for <hongtenzone@foxmail.com>; Tue, 30 Jul 2013 16:53:16 +0800
X-QQ-SSF: 00500000010000010rF000C1040000r
X-QQ-mid: mx38t1375174396thkz17254
Received: from mail-out.sys.baidu.com (cq01-passport-mis00.cq01.baidu.com [10.46.78.11])
    by db-sysnoc-mailrelay3.db01.baidu.com (Postfix) with SMTP id 515EF798060
    for <hongtenzone@foxmail.com>; Tue, 30 Jul 2013 16:53:16 +0800 (CST)
From: =?UTF-8?B?YmFpZHU=?=<passport@baidu.com>
To: =?UTF-8?B?aG9uZ3RlbnpvbmVAZm94bWFpbC5jb20=?=<hongtenzone@foxmail.com>
Subject: =?UTF-8?B?55m+5bqm5LqR6YCB5L2gMTAwR+WtmOWCqOepuumXtOmAmuefpQ==?=
MIME-Version: 1.0
Content-Type: text/html;
    charset="UTF-8"
Content-Transfer-Encoding: base64

PFA+PEEgaHJlZj0iaHR0cDovL3l1bi5iYWlkdS5jb20vMTAwPz1tYWlsMiI+PElNRyBzdHlsZT0i
RElTUExBWTogYmxvY2s7IE1BUkdJTi1MRUZUOiBhdXRvOyBNQVJHSU4tUklHSFQ6IGF1dG8iIHNy
Yz0iaHR0cDovL3Bhbi5iYWlkdS5jb20vcmVzL3N0YXRpYy9pbWFnZXMvY21zL3dvcmxkLTEwMC5q
cGciPjwvQT48Rk9OVCBmYWNlPeW5vOWchj48Rk9OVCBjb2xvcj0jZmYwMDAwIHNpemU9Mz48Rk9O
VCBjb2xvcj0jMDAwMDAwPkhvbmdfdGVuX3pvbmXvvIzmgqjnmoTnmb7luqbkupHlrrnph488Rk9O
VCBjb2xvcj0jZmYwMDAwPjxTVFJPTkc+5Y2z5bCG5Yiw5pyfPC9TVFJPTkc+PC9GT05UPu+8jOiv
t+WwveW/q+e7reacn++8jOS7peWFjeW9seWTjeaCqOWkh+S7veWunei0teaVsOaNruOAgjxTVFJP
Tkc+PEZPTlQgY29sb3I9I2ZmMDAwMD4xMDBH57ut5pyf5Y+q6ZyAMC4y5YWD77yM5Y+y5LiK5pyA
5L2O5Lu3PC9GT05UPjwvU1RST05HPu+8jOa0u+WKqOWcsOWdgO+8mjwvRk9OVD48L0ZPTlQ+PEZP
TlQgY29sb3I9I2ZmMDAwMCBzaXplPTM+PEEgaHJlZj0iaHR0cDovL3l1bi5iYWlkdS5jb20vMTAw
Pz1tYWlsMiI+aHR0cDovL3l1bi5iYWlkdS5jb20vMTAwPC9BPjwvQT48L0ZPTlQ+PC9GT05UPjxG
T05UIHNpemU9Mz48Rk9OVCBjb2xvcj0jZmYwMDAwPiA8L0ZPTlQ+PC9GT05UPjwvUD4NPGJyPjxQ
PjxGT05UIHNpemU9Mj48Rk9OVCBzdHlsZT0iQkFDS0dST1VORC1DT0xPUjogI2ZmZmYwMCIgY29s
b3I9I2ZmMDAwMCBmYWNlPeWui+S9kz7vvIjmnKzkvJjmg6Dnu63mnJ/kuLrmgqjkuJPkuqvnibnm
nYPvvIzor7fli7/ovazlj5HjgIHlpJbms4TvvIzosKLosKLmlK/mjIHvvIHvvIk8L0ZPTlQ+PC9G
T05UPjwvUD4=

下面是我做的demo:

运行效果:

===========================================

代码部分:

===========================================

  1 #python mail
  2 
  3 import os
  4 import email
  5 
  6 #邮件文件的存放位置
  7 PATH = ''
  8 
  9 #读取文件
 10 def read_mail(path):
 11     if os.path.exists(path):
 12         with open(path) as fp:
 13             for line in fp:
 14                 print(line)
 15     else:
 16         not_find_file()
 17 
 18 #打开一个文件   
 19 def open_file(path):
 20     if os.path.exists(path):
 21         return open(path, 'r')
 22     else:
 23         not_find_file()
 24 
 25 #创建消息对象
 26 def get_message(path):
 27     if os.path.exists(path):
 28         fp = open_file(path)
 29         return email.message_from_file(fp)
 30     else:
 31        not_find_file()
 32 
 33 #获取subject对象
 34 def get_subject(path):
 35     if os.path.exists(path):
 36         message = get_message(path)
 37         return message.get('subject')
 38     else:
 39         not_find_file()
 40 
 41 #解析subject对象
 42 def parse_subject(msg):
 43     if msg != None:
 44         subject = msg.get('subject')
 45         #header = email.Header.Header(subject)
 46         #decode_h = email.Header.decode_header(header)
 47         #return decode_h[0][0]
 48     else:
 49         empty_obj()
 50 
 51 #获取发件人邮箱   
 52 def get_from(msg):
 53     if msg != None:
 54         return email.utils.parseaddr(msg.get("from"))[1]
 55     else:
 56         empty_obj()
 57 
 58 #获取收件人邮箱
 59 def get_to(msg):
 60     if msg != None:
 61         return email.utils.parseaddr(msg.get('to'))[1]
 62     else:
 63         empty_obj()
 64 
 65 
 66 #获取邮件的生成时间
 67 def get_date(msg):
 68     if msg != None:
 69         return email.utils.parseaddr(msg.get('date'))[1]
 70     else:
 71         empty_obj()
 72 
 73 #获取邮件的生成版本
 74 def get_mime_version(msg):
 75     if msg != None:
 76         return email.utils.parseaddr(msg.get('mime-version'))[1]
 77     else:
 78         empty_obj()
 79 
 80 #获取邮件的文本类型
 81 def get_content_type(msg):
 82     if msg != None:
 83         return email.utils.parseaddr(msg.get('content-type'))[1]
 84     else:
 85         empty_obj()
 86 
 87 #获取邮件的邮件的id
 88 def get_message_id(msg):
 89     if msg != None:
 90         return email.utils.parseaddr(msg.get('message-id'))[1]
 91     else:
 92         empty_obj()
 93 
 94 
 95 #文件不存在
 96 def not_find_file():
 97     print('文件不存在!')
 98 
 99 #msg is empty
100 def empty_obj():
101     print('msg is empty!')
102     
103 def main():
104     global PATH
105     PATH = 'E:\\test_mail.eml'
106     print(PATH)
107     msg = get_message(PATH)
108     #print(msg)
109     print('#' * 50)
110     print('subject:{}'.format(get_subject(PATH)))
111     print('#' * 50)
112     print(parse_subject(msg))
113     print('#' * 50)
114     print('from:{}'.format(get_from(msg)))
115     print('to:{}'.format(get_to(msg)))
116     print('date:{}'.format(get_date(msg)))
117     print('mime-version:{}'.format(get_mime_version(msg)))
118     print('content-type:{}'.format(get_content_type(msg)))
119     print('message-id:{}'.format(get_message_id(msg)))
120 
121 if __name__ == '__main__':
122     main()
123     

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • python开发_xml.dom_解析XML文档_完整版_博主推荐

    在阅读之前,你需要了解一些xml.dom的一些理论知识,在这里你可以对xml.dom有一定的了解,如果你阅读完之后。

    Hongten
  • python开发_os.path

    =========================================

    Hongten
  • python开发_tkinter_图形随鼠标移动

    ===============================================

    Hongten
  • Django+wechatpy接入微信公众平台以及授权登录

    lin_zone
  • association memory 思路

    hebb memory 将当时场景中同时发生的多种不同信息一起记忆下来,以后场景中特点突出的特征都能提取到此记忆,从此记忆推断出其他相关当时情景中的reward...

    用户1908973
  • 关于SpringBoot bean无法注入的问题

    itliusir
  • 基于python的种子搜索网站(二)开发过程

    该项目是基于python的web类库django开发的一套web网站,做为本人的毕业设计。

    山东程序员
  • linux机器升级python版本至2.

    背景:刚申请的机器python版本是2.6的,需要手动升级到2.7.13版本; 步骤: 1 下载python2.7.13版本的源码包 wget http:/...

    py3study
  • SAP UI5里关于时区问题转换的JavaScript代码

    UI->Backend从local时间转成UTC时间,可以直接使用Date 对象的toUTCString()方法,

    Jerry Wang
  • Ubuntu dpkg ---- apt-get ------ aptitude 区别及作用

    dpkg绕过apt包管理数据库对软件包进行操作,所以你用dpkg安装过的软件包用apt可以再安装一遍,系统不知道之前安装过了,将会覆盖之前dpkg的安装。

    bear_fish

扫码关注云+社区

领取腾讯云代金券