python开发_email_读取邮件头信息

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

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

本文先来谈谈在python中,利用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:

运行效果

python开发_email_读取邮件头信息

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

 

本文转自 Tenderrain 51CTO博客,原文链接:http://blog.51cto.com/tenderrain/1916782



上一篇:form表单的两种提交方式,submit和button的用法


下一篇:Oracle 热备份