需求:在云主机中需要进行发送邮件来监控集群相关的指标信息,动态接收日期参数统计对应日期的数据情况并通过邮件的方式发送出来。
发送邮件的主要方法
通过配置发送人和接收人以及用户名和密码信息调用python的email功能进行发送邮件,需要云主机开外网访问,并且配置端口号465 和587两个端口号
"""
发送邮件主方法,配置邮件收发信息
"""
def sendMail(strs,date):
username="[email protected]"
password="senderpassword"
sender="[email protected]"
receiver=['[email protected]']
# HTML 格式解决乱码问题
msg = MIMEText(strs,_subtype='html',_charset='utf-8')
msg['From'] = sender
msg['To'] = ','.join(receiver)
# 邮件主题
msg['Subject'] = '全优加邮件监控 '+date
# 邮件服务器
smtplib.SMTP_SSL('smtp.exmail.qq.com')
smtp = smtplib.SMTP()
# 邮件服务器端口号,开放465和587端口号进行测试
smtp.connect('smtp.exmail.qq.com',587)
smtp.starttls()
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
邮件中展示表格
"""
将邮件内容添加到HTML当中通过表格的方式展示
"""
def parse_html(date):
global html
content = ''
file = "/data/xxxx/xxxx.out"
print(file)
for line in open(file, 'r'):
splits = line.split(",")
if len(splits) == 8:
content = content + """
<tr>
<td>""" + str(splits[0]) + """</td>
<td>""" + str(splits[1]) + """</td>
<td>""" + str(splits[2]) + """</td>
<td>""" + str(splits[3]) + """</td>
<td>""" + str(splits[4]) + """</td>
<td>""" + str(splits[5]) + """</td>
<td>""" + str(splits[6]) + """</td>
<td>""" + str(splits[7]) + """</td>
</tr>"""
html = """
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
<div id="container">
<p><strong>数据条数监控情况:</strong></p>
<div id="content">
<table width="100%" border="1" bordercolor="black" cellspacing="0" cellpadding="0">
<tr>
<td width="40"><strong>数据库名</strong></td>
<td width="50"><strong>数据表名</strong></td>
<td width="50"><strong>日期</strong></td>
<td width="50"><strong>接收条数</strong></td>
<td width="50"><strong>IDType</strong></td>
<td width="80"><strong>ID名称</strong></td>
<td width="80"><strong>去重ID数</strong></td>
<td width="80"><strong>去重SuperID数</strong></td>
</tr>""" + content + """
</table>
</div>
</div>
</div>
</body>
</html>
"""
完整的python脚本如下:
#coding=utf8
import smtplib
from email.mime.text import MIMEText
import os
import sys
"""
@Date:2020-09-24
@Author:Michealkz
@Description: 邮件发送脚本
@Useage:传入日期作为参数,日期格式:年-月-日
"""
"""
发送邮件主方法,配置邮件收发信息
"""
def sendMail(strs,date):
username="[email protected]"
password="senderpassword"
sender="[email protected]"
receiver=['[email protected]']
# HTML 格式解决乱码问题
msg = MIMEText(strs,_subtype='html',_charset='utf-8')
msg['From'] = sender
msg['To'] = ','.join(receiver)
# 邮件主题
msg['Subject'] = '全优加邮件监控 '+date
# 邮件服务器
smtplib.SMTP_SSL('smtp.exmail.qq.com')
smtp = smtplib.SMTP()
# 邮件服务器端口号,开放465和587端口号进行测试
smtp.connect('smtp.exmail.qq.com',587)
smtp.starttls()
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
"""
将邮件内容添加到HTML当中通过表格的方式展示
"""
def parse_html(date):
global html
content = ''
file = "/data/scripts/bbugo-dev/superid/monitor/"+date+"/MonitoResult.out"
print(file)
for line in open(file, 'r'):
splits = line.split(",")
if len(splits) == 8:
content = content + """
<tr>
<td>""" + str(splits[0]) + """</td>
<td>""" + str(splits[1]) + """</td>
<td>""" + str(splits[2]) + """</td>
<td>""" + str(splits[3]) + """</td>
<td>""" + str(splits[4]) + """</td>
<td>""" + str(splits[5]) + """</td>
<td>""" + str(splits[6]) + """</td>
<td>""" + str(splits[7]) + """</td>
</tr>"""
html = """
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
<div id="container">
<p><strong>SuperID数据条数监控情况:</strong></p>
<div id="content">
<table width="100%" border="1" bordercolor="black" cellspacing="0" cellpadding="0">
<tr>
<td width="40"><strong>数据库名</strong></td>
<td width="50"><strong>数据表名</strong></td>
<td width="50"><strong>日期</strong></td>
<td width="50"><strong>接收条数</strong></td>
<td width="50"><strong>IDType</strong></td>
<td width="80"><strong>ID名称</strong></td>
<td width="80"><strong>去重ID数</strong></td>
<td width="80"><strong>去重SuperID数</strong></td>
</tr>""" + content + """
</table>
</div>
</div>
</div>
</body>
</html>
"""
if __name__ == '__main__':
if len(sys.argv) >0:
date = sys.argv[1]
parse_html(date)
sendMail(html,date)
print("邮件发送成功")
else:
print("请输入日期 格式:年-月-日")
你们在使用邮件发送功能的过程中遇到过什么问题呢?欢迎联系小编进行交流。