[{'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 Imboximport datetimepassword = 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 Workbookworkbook = Workbook() # 创建新工作簿sheet = workbook.activeheading = ['邮件名', '发件人姓名', '发件人邮箱', '发送日期', '发送时间', '邮件正文', '附件']sheet.append(heading)
后面随着邮件的遍历将内容写入表格即可 。案例一的完整代码如下:
import keyringfrom imbox import Imboximport datetimefrom openpyxl import Workbookworkbook = Workbook() # 创建新工作簿sheet = workbook.activeheading = ['邮件名', '发件人姓名', '发件人邮箱', '发送日期', '发送时间', '邮件正文', '附件']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')
推荐阅读
- 从失败到成功的励志故事?励志小故事成功在于坚持_1
- 青桔子怎么做饮料,桔子太青怎么放黄
- 一则励志小故事?一则励志小故事_1
- 跟在身后的小女孩读后感,读后感的格式简单的说
- 电动车|抢宏光mini风头:小电驴重新火起来了
- 到底孕妇吃牛蛙好么
- 男人梦见自己生小孩是什么意思 男人梦见自已生孩子
- 智能手机|天玑8000性能小金刚 Redmi Note 12要改名?卢伟冰在线征名
- 普洱糯米香小沱茶,普洱沱茶的形状
- 土卫六距离地球 土卫六与地球大小比较