发邮件:https://www.cnblogs.com/hudieren/p/16792041.html
收邮件:https://www.cnblogs.com/hudieren/p/16792045.html
解析邮件:https://www.cnblogs.com/hudieren/p/16792096.html
删除邮件:https://www.cnblogs.com/hudieren/p/16798017.html
import imaplib
class EmailOperate:
email_config = {
"sender": "发件人邮箱账号",
"sender_name": "发件人邮箱昵称",
"sender_token": "发件人邮箱密码", # (当时申请smtp给的口令)
"attn": "收件人邮箱账号", # 收件人邮箱账号
"attn_name": "收件人邮箱昵称",
"subject": "主题",
"connect": "内容",
"files": "附件,格式:{'path': '路径', 'read': 'r', 'encoding': 'utf-8', '_charset': 'utf-8', 'name': '文件名'}",
"server": "邮箱服务器地址",
}
postbox = {
"username": "邮箱账号",
"token": "邮箱密码", # (当时申请smtp给的口令)
"server": "邮箱服务器地址",
}
@staticmethod
def delete(postbox, email_uids):
"""
:param postbox:
:param email_uids: 邮件id列表
:return:
"""
username = postbox.get('username')
token = postbox.get('token')
server = postbox.get('server')
# 连接
imap = imaplib.IMAP4_SSL(host=server)
# 登录
imap.login(username, token)
imaplib.Commands['ID'] = ('AUTH')
args = ("name", "zhang3", "contact", f"{username}", "version", "2.58", "vendor", "myclient")
ID_type, ID_result = imap._simple_command('ID', '("' + '" "'.join(args) + '")')
imap.select('INBOX')
error = []
for uid in email_uids:
if isinstance(uid, bytes):
# res = imap.store(uid, '+FLAGS', '\\Deleted')
res = imap.uid("STORE", uid, '+FLAGS', '(\\Deleted)')
if res[0] == "NO":
error.append(uid.decode())
elif isinstance(uid, str):
# res = imap.store(uid.encode(), '+FLAGS', '\\Deleted')
res = imap.uid("STORE", uid.encode(), '+FLAGS', '(\\Deleted)')
if res[0] == "NO":
error.append(uid)
imap.expunge()
imap.close()
imap.logout()
return error