怎么用Python自动化管理邮件
本篇内容主要讲解“怎么用Python自动化管理邮件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Python自动化管理邮件”吧!
目前创新互联公司已为成百上千家的企业提供了网站建设、域名、虚拟空间、网站运营、企业网站设计、黄骅网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
如果你来不及看,至少需要了解下面几个概念
为了体现通用性,这次我们再换一个邮箱讲解,下面的案例均以 88 完美邮箱 为例示范,本文主要将分为下面两个部分介绍
imbox 收取邮件
openpyxl 写入 Excel 文件
案例一
“将邮箱里所有邮件的相关信息存到 Excel 中备份
”要解决这个需求,首先我们利用 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) # 附件
关于上面的代码有几个地方需要注意一下:
Imbox('imap.qq.com', 'xxx@qq.com', password, ssl=True) 这行代码中需要填写服务器、用户名邮箱、密码、SSL加密
循环体中 uid 参数是每封邮件的编号,邮件编号十分重要,可以用于邮件的标记和删除
message.sent_from 返回一个包裹字典的列表,这个字典有两个键:name 和 email,name 即用户名(或昵称),email 是发件人的邮箱
message.date 获取的字符串内容如 'Tue, 3 Nov 2020 08:08:16 +0800 (GMT+08:00)' ,一个 GMT 格式的时间,我们需要将它转化为正常的日期(年月日)+时间(时分秒),也就是将 GMT 时间格式的字符串转换成datetime 类型,可以通过下面的代码:
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
同样需要注意的是
message.body['plain'] 正文以字符串列表形式返回
message.attachments 输出结果举例如下:
[{'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自动化管理邮件”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
当前题目:怎么用Python自动化管理邮件
浏览路径:http://azwzsj.com/article/jdodcd.html