1.改成自己的邮件,和授权码就可以用
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import json
class Send_Email:
def __init__(self,sender_email='xxxx@qq.com'):
# 2.1 定义发件人
self.sender_email = sender_email
def send_msg(self,recer_email,subject,content,data):
print(recer_email,subject,content)
# 3.1 先创建smtp对象
self.smtp_obj = smtplib.SMTP_SSL("smtp.qq.com", 465) # liunx 和 window都可以使用
# 3.2 进行验证,用户授权(对发件人授权) ,登录自己qq邮箱获取非密码
res = self.smtp_obj.login(user=self.sender_email, password="jgkwuswtmacnjfhb123")
# print(res)
# 4.10添加内容
msg = MIMEMultipart()
puretext = MIMEText(content, "plain", 'utf-8')
msg.attach(puretext)
# 4.11添加邮件附件, 不添加可以注释
self.fu(msg, data)
# 4.2 添加收件人
msg['To'] = recer_email
# 4.3 添加发件人
msg['From'] = self.sender_email
# 4.4 添加主题
msg['Subject'] = subject
self.smtp_obj.sendmail(from_addr=self.sender_email, to_addrs=recer_email, msg=msg.as_string())
def send_qunfa(self,recer_email_lst,subject,content,a_data):
if type(recer_email_lst) == list:
for recer_email in recer_email_lst:
self.send_msg(recer_email, subject, content,a_data)
else:
self.send_msg(recer_email_lst, subject, content,a_data)
def fu(self,msg,data):
part = MIMEText(data,'base64', 'utf-8')
part.add_header('Content-Disposition', 'attachment',filename=('utf-8', '', "数据为空详情.txt"))
msg.attach(part)
# part = MIMEApplication(data)
# part.add_header('Content-Disposition', 'attachment', filename="数据为空.txt")
# msg.attach(part)
def __del__(self):
# 关闭
self.smtp_obj.close()
if __name__ == '__main__':
a = {'a_type': '通过', 'ReviewNumbers': 0,}
recer_email = ['xxxxxx@qq.com','xxxxxx@qq.com']
subject = '爬虫任务提醒'
content = json.dumps(a, indent=4, ensure_ascii=False) + '\n' + '通过'
mail = Send_Email()
mail.send_qunfa( recer_email, subject, content,content)