# Author renyupeng # coding=utf-8 # @Time : 2022/02/07 14:43 下午 # @Site : # @File : send_material_report_month.py # @Software: PyCharm # @contact: zhaohailiang@c-top.com.cn # @Tel 1501435553 import datetime from urllib import parse import pandas as pd import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from sqlalchemy import create_engine class send_material_report_month: def __init__(self): self.OuterStr = "mysql+pymysql://%s:%s@%s:%d/%s" % ( "data", parse.quote_plus("hcst@2021"), "139.186.27.96", 3390, "jeecg-boot") self.OuterConn = create_engine(self.OuterStr, pool_size=10, max_overflow=5, pool_timeout=10, pool_recycle=3600) self.stat_date = (datetime.date.today() + datetime.timedelta(-1)).strftime("%Y-%m-%d") def handler(self): sql = """select a.company_name AS '公司', a.project_name AS '项目', a.account_id AS '账户', a.signature AS '素材标识', b.media_time AS '上线时间', a.channel_name '渠道', a.charge AS '消耗', a.user_name AS '运营', a.clip_name AS '剪辑', a.shot_name AS '拍摄', a.plan_name AS '编导', a.leader_name AS '负责人', a.video_name AS '素材名称', a.photo_url AS '链接' from (select company_name,project_name,account_id,signature,channel_name,sum(charge)charge,clip_name,shot_name,plan_name,a.leader_name,video_name,photo_url,b.realname user_name from application.app_kuaishou_video_report_busi_m a left join `jeecg-boot`.sys_user b on a.user_id=b.id where stat_mon=202201 group by account_id,signature) a left join application.app_kuaishou_material_info b on a.signature=b.material_id; """.format(stat_date=self.stat_date) print(sql) path = """/data/excel/kuaishou_material_month.xls""".format(stat_date=self.stat_date) print(path) pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl') my_sender = 'zhaohailiang@c-top.com.cn' # 发件人邮箱账号 my_pass = 'h27UwLdJcD797THk' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码 recipients = ['niuxiaoyu@c-top.com.cn'] # 收件人邮箱账号 # content = "Please check the attachment. " msg = MIMEMultipart() # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便 msg['From'] = my_sender # [收件人邮箱昵称、收件人邮箱账号], 昵称随便 msg['To'] = ",".join(recipients) # 邮件的主题,也就是邮件的标题 msg['Subject'] = "快手-全量素材周报" att1 = MIMEText( open(path, 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' att1["Content-Disposition"] = 'attachment; filename="material_report_weekly.xls"' msg.attach(att1) server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465) server.login(my_sender, my_pass) try: server.sendmail(my_sender, recipients, msg.as_string()) print("发送成功") except Exception as e: print("发送失败",e) if __name__ == '__main__': send_material_report_month().handler()