#邮件发送方,收件方,数据为数据[[1,2],[1,3]]这种数组格式的,邮件主题,发送方密码
# sendAddr = ''
# recipientAddrs = ''
# email_subject = ''
# password = ''
def send_mail_excel(sendAddr,recipientAddrs,alarm_nodes,email_subject,password):
pd.set_option('display.max_colwidth', -1)
columns = ['节点id','节点名称','最后重跑时间','失败次数']
filter_merge_data = pd.DataFrame(alarm_nodes,
columns=columns)
df_html = filter_merge_data.to_html(escape=False) # DataFrame数据转化为HTML表格形式
head = \
"""
<head>
<meta charset="utf-8">
<STYLE TYPE="text/css" MEDIA=screen>
table.dataframe {
border-collapse: collapse;
border: 2px solid #a19da2;
/*居中显示整个表格*/
margin: auto;
}
table.dataframe thead {
border: 2px solid #91c6e1;
background: #f1f1f1;
padding: 10px 10px 10px 10px;
color: #333333;
}
table.dataframe tbody {
border: 2px solid #91c6e1;
padding: 10px 10px 10px 10px;
}
table.dataframe tr {
}
table.dataframe th {
vertical-align: top;
font-size: 14px;
padding: 10px 10px 10px 10px;
color: #105de3;
font-family: arial;
text-align: center;
}
table.dataframe td {
text-align: center;
padding: 10px 10px 10px 10px;
}
body {
font-family: 宋体;
}
h1 {
color: #5db446
}
div.header h2 {
color: #0002e3;
font-family: 黑体;
}
div.content h2 {
text-align: center;
font-size: 28px;
text-shadow: 2px 2px 1px #de4040;
color: #fff;
font-weight: bold;
background-color: #008eb7;
line-height: 1.5;
margin: 20px 0;
box-shadow: 10px 10px 5px #888888;
border-radius: 5px;
}
h3 {
font-size: 22px;
background-color: rgba(0, 2, 227, 0.71);
text-shadow: 2px 2px 1px #de4040;
color: rgba(239, 241, 234, 0.99);
line-height: 1.5;
}
h4 {
color: #e10092;
font-family: 楷体;
font-size: 20px;
text-align: center;
}
td img {
/*width: 60px;*/
max-width: 300px;
max-height: 300px;
}
</STYLE>
</head>
"""
body = \
"""
<body>
<div align="center" class="header">
<!--标题部分的信息-->
<h1 align="center">表格中的数据为当天运行失败超过6次的节点,请查看运维中心处理</h1>
</div>
<hr>
<div class="content">
<!--正文内容-->
<h2> </h2>
<div>
<h4></h4>
{df_html}
</div>
<hr>
<p style="text-align: center">
</p>
</div>
</body>
""".format(yesterday='20211012', df_html=df_html)
html_msg = "<html>" + head + body + "</html>"
html_msg = html_msg.replace('\n', '').encode("utf-8")
msg = email.mime.multipart.MIMEMultipart()
msg['from'] = sendAddr
msg['to'] = recipientAddrs # 多个收件人的邮箱应该放在字符串中,用字符分隔, 然后用split()分开,不能放在列表中, 因为要使用encode属性
msg['subject'] = email_subject
msg.attach(MIMEText(html_msg, "html", "utf-8"))
smtpHost = 'smtp.exmail.qq.com'
port = 465
smtp = smtplib.SMTP_SSL(smtpHost, port) # 需要一个安全的连接,用SSL的方式去登录得用SMTP_SSL,之前用的是SMTP().端口号465或587
smtp.login(sendAddr, password) # 发送方的邮箱,和授权码(不是邮箱登录密码)
smtp.sendmail(sendAddr, recipientAddrs.split(";"), str(msg)) # 注意, 这里的收件方可以是多个邮箱,用";"分开, 也可以用其他符号
smtp.quit()
效果图