惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
H
Help Net Security
Last Week in AI
Last Week in AI
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
Google DeepMind News
Google DeepMind News
B
Blog
C
Check Point Blog
T
Tailwind CSS Blog
云风的 BLOG
云风的 BLOG
D
Docker
Recent Announcements
Recent Announcements
Vercel News
Vercel News
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
人人都是产品经理
人人都是产品经理
月光博客
月光博客
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
The Register - Security
The Register - Security
V
Visual Studio Blog
F
Full Disclosure
Hugging Face - Blog
Hugging Face - Blog
T
Threat Research - Cisco Blogs
Latest news
Latest news
PCI Perspectives
PCI Perspectives
Cisco Talos Blog
Cisco Talos Blog
博客园 - Franky
D
DataBreaches.Net
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
Google Developers Blog
P
Palo Alto Networks Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
T
Tenable Blog
L
LINUX DO - 热门话题
Spread Privacy
Spread Privacy

阿猪的博客

如何使用CNB构建Hexo项目并部署到腾讯云COS 好坑爹!派息后买入的股票怎么也扣股息税? 游哈尔滨 跨年往事 安装Ta-Lib时报错"Could not build wheels" 网站被恶意镜像了该怎么办 延迟退休正式到来!来看看有什么规定 我的基金怎么就被强制卖出了? Hexo-AlgoliaSearch插件报错“Error has occurred during indexing posts” 找工作避坑不完全指南 办理信用卡应该注意什么 华为Pad开启了“共享至电脑”,但是Windows下无法正常访问 webdriver-manager报错一例 Windows无法从环境变量中找到Python的正确位置 习大大2024年新年贺词 号外!号外!Twikoo可以直接屏蔽垃圾评论啦! 如何将Coding的代码仓同步到Github 博客被攻击了 华为Pad开启了“共享至电脑”,但是Ubuntu下无法正常访问 如何恢复Etcher刻录过的U盘 如何在Ubuntu下快速切换网络代理状态 在腾讯云函数中使用Pandas报错`No module named 'numpy.core._multiarray_umath'` 对空的DataFrame使用apply方法未得到预期结果 SQLite中使用关键字作为列名称导致报错 Logging模块重复输出内容的原因及解决方法 如何在Selenium中保持网站的登录状态 如何下载与Chrome浏览器的版本相匹配的ChromeDriver 如何下载旧版本Python的安装包 如何为不同的Python项目自动选择不同的解释器 使用腾讯云函数搭建Web站点 WordPress CORS问题一例 踩坑阿里云函数计算搭建WordPress 在AWS Amplify中部署Jekyll站点 让Chirpy主题支持折叠展示代码块 实现ChatGPT的文字输出效果 python中使用'''注释代码后引起报错 使用pandas_bokeh在地图上显示数据 如何通过复权因子计算复权价格 国内量化平台不完全汇总
使用python自带的email模块解析邮件
阿猪 · 2022-12-16 · via 阿猪的博客

  本文介绍通过Python自带的email模块解析电子邮件的基本实现方法。

  这里阿猪假设你已经通过pop、imap等方式获取到了一封邮件,或者有一个现成的eml文件。下边的代码着重演示解析邮件内容过程中的基本实现方法。

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
import imaplib
from email.parser import Parser
from email.header import decode_header
import re
import datetime

imap_user = '用户名email地址'
imap_object = imaplib.IMAP4_SSL(port="993",host="imap.xxx.com")
imap_object.login(imap_user, '密码或授权码')
imap_object.select('INBOX')
typ, msg_ids = imap_object.search(None, 'ALL')

ids = msg_ids[0]
ret = ids.decode('utf-8')
message_id_list = ret.split()
int_mail_num = len(message_id_list)
print('收件箱中共有%s封邮件'%int_mail_num)

msg = msg_ids[0]
msg_list = msg.split()

ids = msg_list[0]
results, data = imap_object.fetch(ids, "(RFC822)")
imap_object.close()
str_source = data[0][1].decode('UTF-8')



msg_email = Parser().parsestr(str_source)





str_from = msg_email["from"]




str_from_name = re.search(r'(?<=")[\s\S]*?(?=")',str_from).group()
str_from_address = re.search(r'(?<=<)[\s\S]*?(?=>)',str_from).group()
value, charset = decode_header(str_from_name)[0]
if charset:
str_from_name = value.decode(charset)
print(">>From:%s<%s>"%(str_from_name,str_from_address))

str_to = msg_email["to"]
str_to_name = re.search(r'(?<=")[\s\S]*?(?=")',str_to).group()
str_to_address = re.search(r'(?<=<)[\s\S]*?(?=>)',str_to).group()
value, charset = decode_header(str_to_name)[0]
if charset:
str_to_name = value.decode(charset)
print(">>To:%s<%s>"%(str_to_name,str_to_address))

str_date = msg_email["date"]
str_date = str_date.replace('GMT','+0000')
dtime_date = datetime.datetime.strptime(str_date, '%a, %d %b %Y %H:%M:%S %z')
print('>>时间:%s'%dtime_date)

str_subject = msg_email["subject"]
value, charset = decode_header(str_subject)[0]
if charset:
str_subject = value.decode(charset)
print('>>邮件主题:%s'%str_subject)

def decode_mime(msg):
if msg.is_multipart():
parts = msg.get_payload()
for part in parts:
decode_mime(part)
else:
str_content_type = msg.get_content_type()

str_charset = msg.get_content_charset(failobj=None)

if str_content_type in ('text/plain', 'text/html'):
bytes_content = msg.get_payload(decode=True)
str_content = bytes_content.decode(str_charset)
print('>>邮件正文(%s):%s'%(str_content_type,str_content))



decode_mime(msg_email)

版权声明: 未经书面授权许可,任何个人和组织不得以任何形式转载、引用本站的任何内容。本站保留追究侵权者法律责任的权利。

赏杯咖啡,鼓励一下~

  • 微信打赏

    微信打赏