上一篇文章《利用Pycurl库监控WEB访问质量(二)》中成功的实现了通过邮件报警的功能,但是对于发送邮件却有很多的限制:为了邮件美观,一般都会制作成html邮件进行发送。但是发过HTML邮件的同学应该知道,电子邮箱中的HTML页面与普通的HTML页面比较在相当多的限制:
- 不能引用外部CSS,所以只能写在页面里面
- 不能引用外部JS,或者干脆禁用JS脚本
基于上述限制,我在《利用Pycurl库监控WEB访问质量(二)》中将HTML邮件代码直接和Python代码混在一起,细心的同学可以发现代码变得非常的混乱和冗长,实在难以接受。因此想个办法来进行优化。
思路:
- 如何将python代码和HTML代码分离开来?我想到了在平常的WEB开发中,不可避免的会使用到模板。因此模板是否也能适用在我发邮件上呢?经过思考,我决定使用jinja2(http://docs.jinkan.org/docs/jinja2/templates.html) 来使用
- 将HTML代码单独生成一个文件,然后监控脚本上将数据通过参数传递给模板(当然是利用了jinja2模板引擎的功能啦)
- 将最终渲染的html代码通过发送邮件的形式发出来即可。
开始动手吧,第一步生成邮件HTML,将原python文件中的HTML相关代码移出来,保存成mail.html(这我里叫mail.html,可以随意命名)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{{ title }}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0 " />
<meta name="format-detection" content="telephone=no" />
<!--[if !mso]><!-->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet">
<!--<![endif]-->
<style type="text/css">
body {
-webkit-text-size-adjust: 100% !important;
-ms-text-size-adjust: 100% !important;
-webkit-font-smoothing: antialiased !important;
}
img {
border: 0 !important;
outline: none !important;
}
p {
Margin: 0px !important;
Padding: 0px !important;
}
table {
border-collapse: collapse;
mso-table-lspace: 0px;
mso-table-rspace: 0px;
}
td, a, span {
border-collapse: collapse;
mso-line-height-rule: exactly;
}
.ExternalClass * {
line-height: 100%;
}
span.MsoHyperlink {
mso-style-priority:99;
color:inherit;}
span.MsoHyperlinkFollowed {
mso-style-priority:99;
color:inherit;}
@media only screen and (min-width:481px) and (max-width:599px) {
table[class=em_main_table] {
width: 100% !important;
}
table[class=em_wrapper] {
width: 100% !important;
}
td[class=em_hide], br[class=em_hide] {
display: none !important;
}
img[class=em_full_img] {
width: 100% !important;
height: auto !important;
}
td[class=em_align_cent] {
text-align: center !important;
}
td[class=em_pad_top]{
padding-top:20px !important;
}
td[class=em_aside]{
padding-left:10px !important;
padding-right:10px !important;
}
td[class=em_height]{
height: 20px !important;
}
td[class=em_space]{
width:10px !important;
}
td[class=em_width55] {
width:80px !important;
text-align:center !important;
}
td[class=em_width75] {
width:100px !important;
}
td[class=em_font]{
font-size:14px !important;
}
td[class=em_font2] {
text-align:center !important;
}
td[class=em_align_cent1] {
text-align: center !important;
padding-bottom: 10px !important;
}
}
@media only screen and (max-width:480px) {
table[class=em_main_table] {
width: 100% !important;
}
table[class=em_wrapper] {
width: 100% !important;
}
td[class=em_hide], br[class=em_hide], span[class=em_hide] {
display: none !important;
}
img[class=em_full_img] {
width: 100% !important;
height: auto !important;
}
td[class=em_align_cent] {
text-align: center !important;
}
td[class=em_pad_top]{
padding-top:20px !important;
}
td[class=em_height]{
height: 20px !important;
}
td[class=em_aside]{
padding-left:10px !important;
padding-right:10px !important;
}
td[class=em_font]{
font-size:14px !important;
line-height:28px !important;
}
td[class=em_font1]{
font-size:14px !important;
line-height:18px !important;
}
td[class=em_font2]{
font-size:14px !important;
line-height:18px !important;
text-align:center !important;
}
td[class=em_space]{
width:10px !important;
}
span[class=em_br]{
display:block !important;
}
td[class=em_width55] {
width:55px !important;
font-size:15px !important;
line-height:19px !important;
text-align:center !important;
}
td[class=em_width75] {
width:75px !important;
font-size:15px !important;
line-height:19px !important;
}
td[class=em_align_cent1] {
text-align: center !important;
padding-bottom: 10px !important;
}
}
</style>
</head>
<body style="margin:0px; padding:0px;" bgcolor="#ffffff">
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
<!-- BODY -->
<tr>
<td align="center" valign="top" bgcolor="#ffffff">
<table width="600" cellpadding="0" cellspacing="0" border="0" align="center" class="em_main_table" style="table-layout:fixed;">
<!-- === LOGO SECTION === -->
<tr>
<td height="40" class="em_height"> </td>
</tr>
<tr>
<td align="center"><a href="#" target="_blank" style="text-decoration:none;">![LoGo Here](http://upload-images.jianshu.io/upload_images/3900471-558fa222f990324d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></td>
</tr>
<tr>
<td height="30" class="em_height"> </td>
</tr>
<!-- === //LOGO SECTION === -->
<!-- === NEVIGATION SECTION === -->
<tr>
<td height="1" bgcolor="#fed69c" style="font-size:0px; line-height:0px;">![](https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif)</td>
</tr>
<tr>
<td height="1" bgcolor="#fed69c" style="font-size:0px; line-height:0px;">![](https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif)</td>
</tr>
<!-- === //NEVIGATION SECTION === -->
<!-- THANK YOU SECTION -->
<tr>
<td valign="top" class="em_aside">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="27" class="em_height"> </td>
</tr>
<tr>
<td valign="top" align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px;line-height:22px; font-weight:bold; color:#30373b; text-transform:uppercase;">{{ subtitle }}</td>
</tr>
<tr>
<td height="23" class="em_height"> </td>
</tr>
<tr>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">
<table width="290" border="0" cellspacing="0" cellpadding="0" align="left" class="em_wrapper" bgcolor="#f6f7f8">
<tr>
<td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
<span style="color:#feae39;">机房信息 :</span> {{ location }}
</td>
</tr>
</table>
<table width="290" border="0" cellspacing="0" cellpadding="0" align="right" class="em_wrapper">
<tr>
<td valign="top" class="em_pad_top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#f6f7f8" >
<tr>
<td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
<span style="color:#feae39;">机房电话 :</span> {{ jftel}}
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="20" style="font-size:1px; line-height:1px;"> </td>
</tr>
<tr>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#f6f7f8">
<tr>
<td width="25" class="em_space"> </td>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="12" style="font-size:1px; line-height:1px;"> </td>
</tr>
<tr>
<td align="left" valign="top" class="em_font1" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:20px; font-weight:bold; color:#feae39; text-transform:uppercase;">{{ username }} : <span style=" color:#30373b;">{{ telephone}} | {{email}}</span></td>
</tr>
<tr>
<td height="12" style="font-size:1px; line-height:1px;"> </td>
</tr>
</table>
</td>
<td width="25" class="em_space"> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="25" class="em_height"> </td>
</tr>
<tr>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="30" class="em_hide"> </td>
<td width="255" valign="top" align="left" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b; font-weight:bold;">网站</td>
<td width="5"></td>
<td width="35" class="em_width55" valign="top" align="left" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b; font-weight:bold;">Code</td>
<td width="138" class="em_hide"></td>
<td width="5"></td>
<td align="right" class="em_width75" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b; font-weight:bold;">响应时间</td>
<td width="30" class="em_hide"> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="14" style="font-size:1px; line-height:1px;"> </td>
</tr>
<tr>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
{% for site in status %}
<tr>
<td width="30" class="em_hide"> </td>
<td width="255" valign="top" align="left" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;"><a href="http://www.daoluyunshu.com">{{ site[0]}}</a></td>
<td width="5"></td>
<td width="35" class="em_width55" valign="top" align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;">{{ site[1] }}</td>
<td width="138" class="em_hide"></td>
<td width="5"></td>
<td align="right" valign="top" class="em_width75" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;">{{ site[2] }}</td>
<td width="30" class="em_hide"> </td>
</tr>
{% endfor %}
<!-- <tr>
<td width="30" class="em_hide"> </td>
<td width="255" valign="top" align="left" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;"><a href="http://www.daoluyunshu.com">http://www.daoluyunshu.com</a></td>
<td width="5"></td>
<td width="35" class="em_width55" valign="top" align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;">200</td>
<td width="138" class="em_hide"></td>
<td width="5"></td>
<td align="right" valign="top" class="em_width75" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:24px; color:#30373b;">0.287s</td>
<td width="30" class="em_hide"> </td>
</tr> -->
</table>
</td>
</tr>
<tr>
<td height="14" style="font-size:1px; line-height:1px;"> </td>
</tr>
<tr>
<td height="8" class="em_hide" style="font-size:1px; line-height:1px;"> </td>
</tr>
<tr>
<td height="1" bgcolor="#eaebeb" style="font-size:0px;line-height:0px;">![](https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif)</td>
</tr>
<tr>
<td height="14" style="font-size:1px; line-height:1px;"> </td>
</tr>
<tr>
<td height="11" class="em_hide" style="font-size:1px; line-height:1px;"> </td>
</tr>
<tr>
<td valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="right" style="font-family:'Open Sans', Arial, sans-serif; font-size:20px; line-height:24px; color:#30373b; font-weight:bold;">总网站数: {{ num_sites }} 个</td>
<td width="30" class="em_hide"> </td>
</tr>
</table>
</td>
</tr>
<!-- <tr>
<td height="28" class="em_height"> </td>
</tr> -->
</table>
</td>
</tr>
<tr>
<td height="20" class="em_height"> </td>
</tr>
</table>
</td>
</tr>
<!-- //THANK YOU SECTION -->
</table>
</td>
</tr>
<!-- //BODY -->
<!-- === FOOTER SECTION === -->
<tr>
<td align="center" valign="top" bgcolor="#30373b" class="em_aside">
<table width="600" cellpadding="0" cellspacing="0" border="0" align="center" class="em_main_table" style="table-layout:fixed;">
<tr>
<td height="22" class="em_height"> </td>
</tr>
<tr>
<td align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:12px; line-height:18px; color:#848789; text-transform:uppercase;">
<span style="text-decoration:underline;"><a href="#" target="_blank" style="text-decoration:underline; color:#848789;">PRIVACY STATEMENT</a></span> | <span style="text-decoration:underline;"><a href="#" target="_blank" style="text-decoration:underline; color:#848789;">TERMS OF SERVICE</a></span><span class="em_hide"> | </span><span class="em_br"></span><span style="text-decoration:underline;"><a href="#" target="_blank" style="text-decoration:underline; color:#848789;">RETURNS</a></span>
</td>
</tr>
<tr>
<td height="10" style="font-size:1px; line-height:1px;"> </td>
</tr>
<tr>
<td align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:12px; line-height:18px; color:#848789;text-transform:uppercase;">
©2016 XXX.net. All Rights Reserved.
</td>
</tr>
<tr>
<td height="10" style="font-size:1px; line-height:1px;"> </td>
</tr>
<tr>
<td align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:12px; line-height:18px; color:#848789;text-transform:uppercase;">
If you do not wish to receive any further emails from us, please
</td>
</tr>
<tr>
<td height="25" class="em_height"> </td>
</tr>
</table>
</td>
</tr>
<!-- === //FOOTER SECTION === -->
</table>
<div style="display:none; white-space:nowrap; font:20px courier; color:#ffffff; background-color:#ffffff;"> </div>
</body>
</html>
第二步,改造python代码:
# coding: utf-8
import pycurl
try:
from io import BytesIO
except ImportError:
from StringIO import StringIO as BytesIO
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
import jinja2
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
templateLoader = jinja2.FileSystemLoader(searchpath=".")
templateEnv = jinja2.Environment(loader=templateLoader)
TEMPLATE_FILE = 'receipt.html'
template = templateEnv.get_template(TEMPLATE_FILE)
templateVars = {'title': 'XX监控邮件',
'subtitle': 'XX网站监控信息记录',
'location': 'XX数据中心',
'jftel': '138XXXXXXXX',
'username': 'XXX',
'telephone': 'XXXX',
'email': 'XXX@XXX.COM',
'status': [],
'num_sites': 0}
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr(( \
Header(name, 'utf-8').encode(), \
addr.encode("utf-8") if isinstance(addr, unicode) else addr))
from_addr = "XXX@XXX.com"
to_addr = "XXX@XXX.COM" # 发送给多个用户,使用逗号分隔
password = "XXX"
smtp_server = "smtp.XXX.com"
def format_msg(s):
# type: (object) -> object
msg = MIMEText(s, _subtype='html', _charset='utf-8')
msg['From'] = _format_addr(from_addr)
msg['To'] = _format_addr(to_addr)
msg['Subject'] = Header(u'来自XXX的监控小黑屋', 'utf-8').encode()
return msg.as_string()
def send_email(s):
server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(0)
server.login(from_addr, password)
server.sendmail(from_addr,to_addr, s)
server.quit()
flag = False # 决定是否发报警邮件的标志
list_of_sites = []
outputText = ''
with open("sites.txt") as f:
list_of_sites = f.readlines()
num_sites = len(list_of_sites)
templateVars['num_sites'] = num_sites
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.FRESH_CONNECT, 1)
for site in list_of_sites:
if len(site) == 0 or site.startswith("#"):
pass
else:
c.setopt(c.URL, site.replace('\n', ''))
c.setopt(c.WRITEFUNCTION, buffer.write)
try:
c.perform()
http_code = c.getinfo(c.HTTP_CODE)
total_time = c.getinfo(c.TOTAL_TIME)
if http_code == 200:
if total_time <= 5.0:
templateVars['status'].append([site, http_code, total_time])
# outputText = template.render(templateVars)
else:
templateVars['status'].append([site, http_code, total_time])
# outputText = template.render(templateVars)
flag = True
else:
templateVars['status'].append([site, http_code, total_time])
# outputText = template.render(templateVars)
flag = True
except pycurl.error, error:
errno, errstr = error
print errno, errstr
templateVars['status'].append([site, errno, errstr])
# outputText = template.render(templateVars)
flag = True
c.close()
print templateVars
outputText = template.render(templateVars)
msg = format_msg(outputText)
if flag:
send_email(msg)
最后的邮件如下: