先看效果
完整代码
import smtplib
from email.mime.text import MIMEText
class SendMail:
def __init__(self):
self.username = "xxx"
self.pw = "xxx"
self.from_addr = self.username
self.smtp = "smtp.exmail.qq.com"
self.smtp_port = 465
def login(self):
s = smtplib.SMTP_SSL(self.smtp, self.smtp_port)
s.login(self.username, self.pw)
return s
def send_to_audit(self, order_user, order_id, reason, sql, to_addr_list):
try:
msg_template_audit = """
申请理由:
{reason}
待审批的Sql:
{sql}
同意
拒绝
""".format(sql=sql, reason=reason)
msg = MIMEText(msg_template_audit, "html", 'utf-8')
msg["Subject"] = "{0}发起Sql工单号为{1}的执行申请".format(order_user, order_id)
msg["From"] = self.from_addr
msg["To"] = ""
self.login().sendmail(from_addr=self.from_addr, to_addrs=to_addr_list, msg=msg.as_string())
print("send audit mail success")
except Exception as e:
print(e)
def send_to_notify(self, order_id, result, msg, to_addr_list):
try:
"""
$green: #82CF85;
$red: #F57E7D;
$blue: #7db8f5;
$orange: #FFAC69;
"""
if result == "success":
color = "#82CF85"
elif result == "execute":
color = "#7db8f5"
else:
color = "#F57E7D"
msg_template_notify = """
Hello! {msg}!
""".format(result=result, color=color, msg=msg)
msg = MIMEText(msg_template_notify, "html", 'utf-8')
msg["Subject"] = "工单号{0}待执行".format(order_id) if result == "execute" else "Sql工单号为{0}的执行结果".format(order_id)
msg["From"] = self.from_addr
msg["To"] = ""
self.login().sendmail(from_addr=self.from_addr, to_addrs=to_addr_list, msg=msg.as_string())
print("send notify mail success")
except Exception as e:
print(e)
# SendMail().send_to_audit("hugo", 12, "hot fix", "select * from app-api", ["
[email protected] "])
SendMail().send_to_notify(12, "execute", "工单号12待执行", ["
[email protected] "])
这是一个不错的细说https://www.jianshu.com/p/abb2d6e91c1f