在實(shí)際工作中,經(jīng)常有需要定時(shí)發(fā)郵件的任務(wù)。例如對某些系統(tǒng)進(jìn)行掃描,并發(fā)送相應(yīng)的掃描報(bào)告等。
本文基于對Jira系統(tǒng)issues的基本掃描,并發(fā)送郵件報(bào)告的應(yīng)用作為簡單示例。
具體python對Jira的一些操作詳見Python操作Jira
SMTP客戶端
smtplib模塊是python中smtp(簡單郵件傳輸協(xié)議)的客戶端實(shí)現(xiàn)。我們可以使用smtplib模塊,輕松的發(fā)送電子郵件。
下面代碼是我整合smtplib相關(guān)method實(shí)現(xiàn)的一個(gè)發(fā)送郵件的方法。
import smtplib
def send_email(mail_user, mail_password, mail_host, port, receivers, message):
smtp = smtplib.SMTP(mail_host) # 創(chuàng)建一個(gè)smtp客戶端對象
smtp.connect(mail_host, port) # 連接服務(wù)器
smtp.ehlo() # 用戶認(rèn)證
smtp.starttls() # 明文通信協(xié)議的擴(kuò)展,能夠讓明文的通信連線直接成為加密連線(使用SSL或TLS加密),而不需要使用另一個(gè)特別的端口來進(jìn)行加密通信,屬于機(jī)會性加密
smtp.login(mail_user, mail_password) # 登陸
smtp.sendmail(mail_user, receivers, message.as_string()) # 發(fā)送郵件
smtp.quit() # 退出
參數(shù)解釋:
-
mail_user和mail_password是你郵件的用戶名(一般為郵箱地址)和密碼 -
mail_host和port:SMTP服務(wù)器地址和端口 -
receivers:收件人(列表) -
message:郵件內(nèi)容,MIMEMultipart對象
email庫構(gòu)建郵件內(nèi)容
前面已經(jīng)實(shí)現(xiàn)一個(gè)發(fā)送郵件的客戶端接口,接下來就是郵件內(nèi)容的構(gòu)建,主要是用email庫。
# 導(dǎo)入相關(guān)庫-email
from email.mime.multipart import MIMEMultipart # 構(gòu)建郵件頭信息,包括發(fā)件人,接收人,標(biāo)題等
from email.mime.text import MIMEText # 構(gòu)建郵件正文,可以是text,也可以是HTML
from email.mime.application import MIMEApplication # 構(gòu)建郵件附件,理論上,只要是文件即可,一般是圖片,Excel表格,word文件等
from email.header import Header # 專門構(gòu)建郵件標(biāo)題的,這樣做,可以支持標(biāo)題中文
構(gòu)建正文
# 郵件頭信息
message = MIMEMultipart("related")
message["Subject"] = Header("Test", 'utf-8')
message["From"] = "xxx"
message["To"] = ','.join(receivers)
# 郵件內(nèi)容
html_msg = parse_to_html(headers=headers, data=data) # 自定義接口構(gòu)建html格式正文
content_text = MIMEText(html_msg, 'html', 'utf-8')
message.attach(content_text)
將字典對象整合嵌套進(jìn)html,方便展示數(shù)據(jù),包括合并單元格。
def parse_to_html(headers, data):
html_msg = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style type="text/css">
table.imagetable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
}
table.imagetable th {
background:#b5cfd2 url('cell-blue.jpg');
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
table.imagetable td {
background:#dcddc0 url('cell-grey.jpg');
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
</style>
<title></title>
</head>
<body>
<table class="imagetable">
%s
%s
</table>
</body>
</html>
"""
thead = "<thead><tr>"
# 構(gòu)建表格頭
for header in headers:
thead += "<th>" + header + "</th>"
thead += "</tr></thead>"
tbody = "<tbody>"
# 構(gòu)建tbody
for key, value in data.items():
row_span = len(value) # 合并行單元格
tbody += '<tr><td rowspan="%d">%s</td>' % (row_span, key)
for sub_value in value[0].values():
if isinstance(sub_value, list):
tbody += "<td>" + ", ".join(sub_value) + "</td>"
else:
tbody += "<td>" + sub_value + "</td>"
tbody += "</tr>"
for item in value[1:]:
tbody += "<tr>"
for sub_value in item.values():
if isinstance(sub_value, list):
tbody += "<td>" + ", ".join(sub_value) + "</td>"
else:
tbody += "<td>" + sub_value + "</td>"
tbody += "</tr>"
tbody += "</tbody>"
html_msg = html_msg % (thead, tbody)
return html_msg
參考
http://m.itdecent.cn/p/5eb10cef74d0
https://blog.csdn.net/tonvchong/article/details/25985773