自动发送邮件脚本

浏览: 1917

image.png

这篇博客把每天自动发送邮件的脚本讲一讲吧,虽然很基础,好像没什么可以说的 ...

在日常运营工作中,数据提取人员面对众多业务方的数据需求,往往应接不暇。他们需要一套自动化的程序去帮助他们完成一些周期性和重复性较强的工作。

为了减少重复性工作,数据提取人员可以使用Python自动化脚本跑定时任务。将写好的HQL语句放入Python脚本中,并在linux服务器上设置crontab定时调度任务,保证每天定时自动从数据仓库提取数据完毕后,将结果集写到excel中并发送邮件到数据需求方的邮箱。Python脚本代码执行如下

#coding: utf-8
search_data = """ 创建临时表查询昨日运营数据"""
report_data = ''' select * from 上一步创建的临时表 '''

import psycopg2
import smtplib
import os
import openpyxl
import datetime
from impala.dbapi import connect
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import pyhs2 # HIVE环境

wb = openpyxl.load_workbook('/home/path/username/daily_report_v1.xlsx') # 打开服务器存储路径下的excel文件

# 连接HIVE环境
impala_conn = pyhs2.connect(host='10.xx.xx.xx', port=xxx, authMechanism="PLAIN", user='username', password='password', database='dwd')

seo_h5_1 = impala_conn.cursor()
h5_result = impala_conn.cursor()

seo_h5_1.execute('''SET mapreduce.job.queuename=root.yydata''')
seo_h5_1.execute(search_data) # 执行HQL语句

# 取出来数据
h5_result.execute(report_data) # 取出来数据
h5_result = h5_result.fetchall()

#放到sheet里面去
sheet = wb.get_sheet_by_name('daily_report') #daily_report表

#清除历史数据
for i in range(2,sheet.max_row + 1 ):
for j in range(1,sheet.max_column + 1 ):
sheet.cell(row=i,column=j).value = ''

#填充结果数据
for i in range(2,len(h5_result) + 2 ):
for j in range(1,len(h5_result[i-2]) + 1 ):
sheet.cell(row=i,column=j).value = h5_result[i-2][j-1]

#关闭HIVE链接
impala_conn.close()
wb.save('/home/path/usernamet/daily_report_v1.xlsx') # 保存excel文件
receiver = 'receiver_email@xxx.com' # 收件人邮箱地址

date_str = datetime.datetime.strftime(datetime.date.today()-datetime.timedelta(days=1),'%m%d')

mail_txt = """
Dear All,
附件是运营日报,请查收。
"""
msgRoot = MIMEMultipart('mixed')
msgRoot['Subject'] = unicode(u'日报-%s' % date_str) #添加日期
msgRoot['From'] = 'sender_email@xxx.com'
msgRoot['To'] = receiver
msgRoot["Accept-Language"]="zh-CN"
msgRoot["Accept-Charset"]="ISO-8859-1,utf-8"

msg = MIMEText(mail_txt,'plain','utf-8')
msgRoot.attach(msg)
att = MIMEText(open('/home/path/usernamet/daily_report_v1.xlsx', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="日报2017%s.xlsx"' % date_str
msgRoot.attach(att)
smtp = smtplib.SMTP()
smtp.connect('mail.address.com')
smtp.login('sender_email@xxx.com', 'sender_password')
for k in receiver.split(','):
smtp.sendmail('receiver_email@xxx.com', k, msgRoot.as_string())
smtp.quit()

将上面的python脚本后放入连接到数据仓库的服务器上,在linux下设置crontab调度语句,如“10 16 * * * python /home/path/username/auto_email.py”表示每天下午16点10分执行/home/ path/username/路径下的auto_email.py文件。

执行代码后,程序将自动执行SQL语句连接到数据库提取数据,提数完毕后将数据写入excel文件中,并自动发送邮件到数据需求方邮箱。

这样通过定时调度的脚本即可解决业务方每天对日报数据的需求,将数据提取人员从繁重的机械性劳动中解放出来

推荐 0
本文由 watermelon 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册