先看效果
完整代码
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 = """
<div style=" padding: 1em;">
<div style="display: table;caption-side: top;width: 100%;">
<div style="padding-top: 10px">
<h1 style="display: inline-block; margin-right: 2em;">申请理由:</h1>
<p>{reason}</p>
<h2 style="display: inline-block; margin-right: 2em;">待审批的Sql:</h2>
<p>{sql}
</p>
</div>
<nav style="display: table-caption;" role="navigation">
<ol style="display: table-row;">
<li style=" display: inline-block; margin-right: 1em;"><a href="https://www.baidu.com" target="_blank">同意</a></li>
<li style=" display: inline-block; argin-right: 1em;"><a href="#" target="_blank">拒绝</a></li>
</ol>
</nav>
</div>
</div>
</div>
</div>
""".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 = """
<div style="position: relative;
display: block;
padding: 1rem;
margin: 1rem;
background: #E4EAEE;
border: 1px solid rgba(black,.15);
border-radius: 2px;
color: rgba(black,.5);position: relative;
display: block;
padding: 1rem;
margin: 1rem;
background: {color};
border: 1px solid rgba(black,.15);
border-radius: 2px;
color: rgba(black,.5);">
<strong>Hello!</strong> {msg}!
</div>
""".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", ["chenpengren@0easy.com"])
SendMail().send_to_notify(12, "execute", "工单号12待执行", ["chenpengren@0easy.com"])
这是一个不错的细说https://www.jianshu.com/p/abb2d6e91c1f