The Latest 5 Days Footfall
—— report end ——
Best Regards STL Data Service#!/usr/bin/python
import pandas as pd
import datetime
import smtplib
from email.mime.text import MIMEText
#from django.core.mail import EmailMultiAlternatives
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
from email.header import make_header
import os
import ssl
pd.set_option(‘display.max_colwidth’, -1) # 能显示的最大宽度, 否则to_html出来的地址就不全
import sys
reload(sys)
sys.setdefaultencoding(‘utf8’)
def get_html_msg():
“”"
1.构造html信息
“”"
df =pd.read_excel("")
df_html=df.to_html(escape=False)
head = \
"""
"""
# 构造模板的附件(100)
body = \
"""
Dears,
Below is the trend of the footfall 2020 for your reference
Thanks for your time to read
This report is generated and sent automatially
The Latest 5 Days Footfall
{df_html}
—— report end ——
Best Regards
STL Data Service
""".format(df_html=df_html)
html_msg= "" + head + body + ""
# 这里是将HTML文件输出,作为测试的时候,查看格式用的,正式脚本中可以注释掉
return html_msg
#msg.attach(html_msg)
def getYesterday():
today = datetime.date.today()
oneday=datetime.timedelta(days=1)
yesterday=today-oneday
# 日期转字符串
partition_date=yesterday.strftime('%d %b %y')
return partition_date
def send_data(html_msg):
smtp_server = ‘’
username = “”
# 邮件发送和接收人
sender = username
receiver = ['']
partition_date=getYesterday()
# 邮件头信息
msg = MIMEMultipart('related')
#yesterday=getYesterday()
msg['Subject'] =Header('Footfall Update on '+partition_date)
msg["From"] = sender
msg['To'] = ','.join(receiver) # 这里要注意
# html 内容
content_html = MIMEText(html_msg, "html", "utf-8")
msg.attach(content_html)
#发送图片
fp = open('图片路径','rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID','')
msg.attach(msgImage)
# 发送邮件,测试成功,流程都是固定的:创建客户端,登陆,发送,关闭
email_client = smtplib.SMTP(smtp_server,25)
#email_client.login(username, password)#因为是内部服务器就不需要账号登陆了。
email_client.sendmail(sender, receiver, msg.as_string())
email_client.quit()
if name == ‘main’:
html_msg = get_html_msg()
send_data(html_msg)