| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 | # Author renyupeng# coding=utf-8# @Time    : 2021/12/20 11:18 上午# @Site    : # @File    : send_material_report_weekly.py.py# @Software: PyCharm# @contact: renyupeng@c-top.com.cn# @Tel 1501435553import datetimefrom urllib import parseimport pandas as pdimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom sqlalchemy import create_engineclass send_material_report_weekly:    def __init__(self):        self.OuterStr = "mysql+pymysql://%s:%s@%s:%d/%s" % (            "hcst", parse.quote_plus("hcst@2021"), "192.168.0.184", 3390, "application")        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 stat_date `日期`,       advertiser_id `账户`,       city `城市`,       charge `消耗`,       aclick `曝光`,       bclick  `行为`,       activation `激活`,       event_pay `付费`,       event_new_user_pay `新增付费人数`from media_api.kuaishou_audience_city_report_dailywhere stat_date = {stat_date}and advertiser_id in(12215822,12267701,12267702)""".format(stat_date=self.stat_date)        print(sql)        path = """/data/excel/city_{stat_date}.xls""".format(stat_date=self.stat_date)        print(path)        pd.read_sql(sql, self.OuterConn).to_excel(path, index=False, engine='openpyxl')        my_sender = 'renyupeng@c-top.com.cn'  # 发件人邮箱账号        my_pass = 'fcyJKkHUyPo6Zztw'  # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码        recipients = ['yanmenglian@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="city_data.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_weekly().handler()
 |