200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > Python带HTML表格图片的自动邮件发送

Python带HTML表格图片的自动邮件发送

时间:2023-09-26 10:23:26

相关推荐

Python带HTML表格图片的自动邮件发送

#!/usr/bin/python

-- coding: UTF-8 --

smtplib 用于邮件的发信动作

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

email 用于构建邮件内容

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 = \"""<head><meta charset="utf-8"><STYLE TYPE="text/css" MEDIA=screen>table.dataframe {border-collapse: collapse;border: 2px solid #a19da2;/*居中显示整个表格*/margin: auto;}table.dataframe thead {border: 2px solid #91c6e1;background: #f1f1f1;padding: 10px 10px 10px 10px;color: #333333;}table.dataframe tbody {border: 2px solid #91c6e1;padding: 10px 10px 10px 10px;}table.dataframe tr {}table.dataframe th {vertical-align: top;font-size: 14px;padding: 10px 10px 10px 10px;color: #105de3;font-family: arial;text-align: center;}table.dataframe td {text-align: center;padding: 10px 10px 10px 10px;}body {font-family: 宋体;}h1 {color: #5db446}div.header h2 {color: #0002e3;font-family: Calibri;}div.content h2 {text-align: center;font-size: 28px;text-shadow: 2px 2px 1px #de4040;color: #fff;font-weight: bold;background-color: #008eb7;line-height: 1.5;margin: 20px 0;box-shadow: 10px 10px 5px #888888;border-radius: 5px;}h3 {font-size: 22px;background-color: rgba(0, 2, 227, 0.71);text-shadow: 2px 2px 1px #de4040;color: rgba(239, 241, 234, 0.99);line-height: 1.5;}h4 {color: #e10092;font-family: 楷体;font-size: 20px;text-align: center;}td img {/*width: 60px;*/max-width: 300px;max-height: 300px;}</STYLE></head>"""# 构造模板的附件(100)body = \"""<body><div align="left" class="header"><!--标题部分的信息--><p style="font-family:arial;color:blue;font-size:20px;">Dears,</p><p style="font-family:arial;color:black;font-size:20px;">Below is the trend of the footfall for your reference<br>Thanks for your time to read<br><h1 align="center">This report is generated and sent automatially </h1></div><hr><div class="content"><!--正文内容--><h2>The Latest 5 Days Footfall</h2><div><h4></h4>{df_html}</div><hr><p style="text-align: center"> <br><img src="cid:image1"></br><p style="text-align: center">—— report end ——</p>Best Regards</br>STL Data Service</div></body>""".format(df_html=df_html)html_msg= "<html>" + head + body + "</html>"# 这里是将HTML文件输出,作为测试的时候,查看格式用的,正式脚本中可以注释掉

fout = open(‘t4.html’, ‘w’, encoding=‘UTF-8’, newline=’’)

fout.write(html_msg)

return html_msg

#msg.attach(html_msg)

def getYesterday():

“”"

:return: 获取昨天日期

“”"

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 = usernamereceiver = ['']partition_date=getYesterday()# 邮件头信息msg = MIMEMultipart('related')#yesterday=getYesterday()msg['Subject'] =Header('Footfall Update on '+partition_date)msg["From"] = sendermsg['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','<image1>')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()

ifname== ‘main’:

html_msg = get_html_msg()

send_data(html_msg)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。