Python 官方文档:入门教程 => 点击学习
在python中的类库中,python自带了email模块。在email模块中,我们可以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:
运行效果:
#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()
--结束END--
本文标题: python开发_email_读取邮件头
本文链接: https://lsjlt.com/news/185981.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0