来源:早起Python/ target=_blank class=infotextkey>Python
作者:自动化工作组
大家好,我是早起。
在之前的文章中我们已经了解如何对自己的邮箱做一些代码操作前的基础配置,以及通过 Python 代码收发、读取邮件。本文将进一步讲解如何利用Python备份邮件,主要将涉及三种不同的形式
“备份邮箱全部邮件备份指定用户邮件备份指定内容邮件”
之前的文章中我们也提到,基础配置需要打开 POP3/SMTP/IMAP,针对不同邮箱有不同的操作,并以网易 163 邮箱和 QQ 邮箱 举例说明,其他邮箱设置基本类似。详情可以查看我之前的Python办公自动化系列文章。
如果你来不及看,至少需要了解下面几个概念
为了体现通用性,这次我们再换一个邮箱讲解,下面的案例均以 88 完美邮箱 为例示范,本文主要将分为下面两个部分介绍
首先来看一个最通用的,将邮箱中的全部邮件备份至本地。
要解决这个需求,首先我们利用 keyring 库,通过系统密钥环将开启 IMAP/SMTP服务后获得的密码(授权码)预先在本地存储好。首先打开命令行输入python后输入:
import keyring
keyring.set_password("88mail", "test@88.com", "password")
这样 password 在本地存储好了,后面只需要 keyring.get_password 获取作为变量即可:
import keyring
password = keyring.get_password('88mail', 'test@88.com')
打开 88 邮箱的网页版,进一步确认是否配置是否完成,以及服务器邮箱:
用 imbox 读取邮件的代码如下:
import keyring
from imbox import Imbox
password = keyring.get_password('88mail', 'test@88.com')
with Imbox('imap.88.com', 'test@88.com', password, ssl=True) as imbox:
# 获取全部邮件
all_inbox_messages = imbox.messages() # 获取全部邮件
for uid, message in all_inbox_messages:
print(uid) # 邮件编号
print(message.sent_from) # 发件人
print(message.subject) # 邮件主题
print(message.date) # 日期
print(message.body['plain']) # 邮件文本格式正文
print(message.attachments) # 附件
关于上面的代码有几个地方需要注意一下:
import datetime
date = 'Tue, 3 Nov 2020 08:08:16 +0800 (GMT+08:00)'
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S +0800 (GMT+08:00)'
print(datetime.datetime.strptime(date, GMT_FORMAT))
# 2020-11-03 08:08:16
同样需要注意的是
[{'content-type': 'Application/x-zip-compressed', 'size': 1160851, 'content': <_io.BytesIO object at 0x046A7B68>, 'filename': 'xxxx.zip'}]
也是一个字典列表,一个附件为一个字典,包括 content-type size content filename 4 个键,即类型、大小、内容、文件名。若无附件则返回空列表。
有了上面的知识我们就可以重新把邮件的关键信息都解析成字符串形式方便存储:
import keyring
from imbox import Imbox
import datetime
password = keyring.get_password('88mail', 'test@88.com')
with Imbox('imap.88.com', 'test@88.com', password, ssl=True) as imbox:
all_inbox_messages = imbox.messages() # 获取全部邮件
for uid, message in all_inbox_messages:
name = message.sent_from[0]['name'] # 发件人姓名
email = message.sent_from[0]['email'] # 发件人邮箱
title = message.subject
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S +0800 (GMT+08:00)'
email_datetime = str(datetime.datetime.strptime(message.date, GMT_FORMAT))
email_date = email_datetime.strip()[0] # 发送日期
email_time = email_datetime.strip()[1] # 发送时间
text = message.body['plain'] # 文本格式正文
attachment_lst = []
attachments = ''
if message.attachments:
for attachment in message.attachments:
attachment_lst.append(attachment['filename'])
attachments = ', '.join(attachment_lst)
print(uid, name, email, title, email_date, email_time)
print(text, attachments if attachments else '无')
接着利用 openpyxl 写入上述信息即可,首先在程序开头创建好表头,明确存储的信息:
from openpyxl import Workbook
workbook = Workbook() # 创建新工作簿
sheet = workbook.active
heading = ['邮件名', '发件人姓名', '发件人邮箱', '发送日期', '发送时间', '邮件正文', '附件']
sheet.append(heading)
后面随着邮件的遍历将内容写入表格即可。案例一的完整代码如下:
import keyring
from imbox import Imbox
import datetime
from openpyxl import Workbook
workbook = Workbook() # 创建新工作簿
sheet = workbook.active
heading = ['邮件名', '发件人姓名', '发件人邮箱', '发送日期', '发送时间', '邮件正文', '附件']
sheet.append(heading)
password = keyring.get_password('88mail', 'test@88.com')
with Imbox('imap.88.com', 'test@88.com', password, ssl=True) as imbox:
all_inbox_messages = imbox.messages() # 获取全部邮件
for uid, message in all_inbox_messages:
name = message.sent_from[0]['name'] # 发件人姓名
email = message.sent_from[0]['email'] # 发件人邮箱
title = message.subject
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S +0800 (GMT+08:00)'
email_datetime = str(datetime.datetime.strptime(message.date, GMT_FORMAT))
email_date = email_datetime.strip()[0] # 发送日期
email_time = email_datetime.strip()[1] # 发送时间
text = message.body['plain'] # 文本格式正文
attachment_lst = []
attachments = ''
if message.attachments:
for attachment in message.attachments:
attachment_lst.append(attachment['filename'])
attachments = ', '.join(attachment_lst)
sheet.append([title, name, email, email_date, email_time, text, attachments])
workbook.save('xxxxx.xlsx')
现在来看进阶一点的需求,备份指定用户的邮件,比如
“
备份收件箱中所有小明(xiaoming@qq.com)发来的邮件至 Excel,然后删除这些邮件
”
其实如果顺利理解了案例一,那么这个案例就特别简单了 从上面的案例中我们知道,通过下面的代码就可以获取发件人邮箱:
with Imbox('imap.88.com', 'test@88.com', password, ssl=True) as imbox:
all_inbox_messages = imbox.messages()
for uid, message in all_inbox_messages:
email = message.sent_from[0]['email'] # 发件人邮箱
那么只需要在这个基础上判断 if email == 'xiaoming@qq.com' 就可以往后执行相应代码了。另外,删除邮件是基于邮箱编号 uid 的,代码为 imbox.delete(uid) ,具体代码举例:
for uid, message in all_inbox_messages:
if 满足某种条件的邮件:
imbox.delete(uid)
那么,我们综合上述两个知识点以及案例一的知识延伸,就很好写出案例二的代码了:
import keyring
from imbox import Imbox
import datetime
from openpyxl import Workbook
workbook = Workbook()
sheet = workbook.active
heading = ['邮件名', '发件人姓名', '发件人邮箱', '发送日期', '发送时间', '邮件正文', '附件']
sheet.append(heading)
password = keyring.get_password('88mail', 'test@88.com')
with Imbox('imap.88.com', 'test@88.com', password, ssl=True) as imbox:
all_inbox_messages = imbox.messages()
for uid, message in all_inbox_messages:
email = message.sent_from[0]['email']
if email == 'xiaoming@qq.com': # 在此处判断发件人
name = message.sent_from[0]['name']
title = message.subject
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S +0800 (GMT+08:00)'
email_datetime = str(datetime.datetime.strptime(message.date, GMT_FORMAT))
email_date = email_datetime.strip()[0]
email_time = email_datetime.strip()[1]
text = message.body['plain']
attachment_lst = []
attachments = ''
if message.attachments:
for attachment in message.attachments:
attachment_lst.append(attachment['filename'])
attachments = ', '.join(attachment_lst)
sheet.append([title, name, email, email_date, email_time, text, attachments])
imbox.delete(uid) # 在此处删除符合要求的邮件
workbook.save('xxxxx.xlsx')
上一个需求是备份指定用户的邮件,现在也可以备份包含指定内容的邮件,例如
“
备份收件箱中标题含有 “奖金” 的邮件至 Excel,然后红旗标记这些邮件
”
基本思路跟案例二类似,不过多展开讲,只复习两个知识点。要判断标题是否有“奖金”二字:
with Imbox('imap.88.com', 'test@88.com', password, ssl=True) as imbox:
all_inbox_messages = imbox.messages()
for uid, message in all_inbox_messages:
title = message.subject
if '奖金' in title:
pass
红旗标记邮件也删除邮件一样,也是基于邮箱编号 uid 的,具体代码举例:
for uid, message in all_inbox_messages:
if 满足某种条件的邮件:
imbox.mark_flag(uid)
故案例三的完整代码如下:
import keyring
from imbox import Imbox
import datetime
from openpyxl import Workbook
workbook = Workbook()
sheet = workbook.active
heading = ['邮件名', '发件人姓名', '发件人邮箱', '发送日期', '发送时间', '邮件正文', '附件']
sheet.append(heading)
password = keyring.get_password('88mail', 'test@88.com')
with Imbox('imap.88.com', 'test@88.com', password, ssl=True) as imbox:
all_inbox_messages = imbox.messages()
for uid, message in all_inbox_messages:
title = message.subject
if '奖金' in title: # 判断标题是否含指定内容
name = message.sent_from[0]['name']
email = message.sent_from[0]['email']
GMT_FORMAT = '%a, %d %b %Y %H:%M:%S +0800 (GMT+08:00)'
email_datetime = str(datetime.datetime.strptime(message.date, GMT_FORMAT))
email_date = email_datetime.strip()[0]
email_time = email_datetime.strip()[1]
text = message.body['plain']
attachment_lst = []
attachments = ''
if message.attachments:
for attachment in message.attachments:
attachment_lst.append(attachment['filename'])
attachments = ', '.join(attachment_lst)
sheet.append([title, name, email, email_date, email_time, text, attachments])
imbox.mark_flag(uid) # 在此处标记符合要求的邮件
workbook.save('xxxxx.xlsx')
以上就是通过Python实现邮件管理自动化的三个实用案例,完整代码都已经给出,感兴趣的读者可以自行尝试!