import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
filename='C:\\Users\\thinkpad\\Desktop\\1.xlsx'
#附件地址
def send_mail(to_list,sub,context,filename): #to_list:收件人;sub:主题;content:邮件内容
mail_host="smtp.163.com" #设置服务器
mail_user="
[email protected]" #用户名
mail_pass="xxxxxx" #口令
mail_postfix="163.com" #发件箱的后缀
me="服务器"+"<"+mail_user+"@"+mail_postfix+">" #这里的“服务器”可以任意设置,收到信后,将按照设置显示
msg = MIMEMultipart()
#给定msg类型
msg['Subject'] = sub
#邮件主题
msg['From'] = me
msg['To'] = ";".join(mailto_list)
msg.attach(context)
#构造附件1
att1 = MIMEText(open(filename, 'rb').read(), 'xls', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment;filename='+filename[-6:]#这里的filename可以任意写,写什么名字,邮件中显示什么名字,filename[-6:]指的是之前附件地址的后6位
msg.attach(att1)
try:
s = smtplib.SMTP()
s.connect(mail_host) #连接smtp服务器
s.login(mail_user,mail_pass) #登陆服务器
s.sendmail(me, mailto_list, msg.as_string()) #发送邮件
s.close()
return True
except Exception:
return False
if __name__ == '__main__':
mailto_list=["
[email protected]"]
a=pd.DataFrame({'数列1':(1,1,1,1),'数列2':(2,2,2,2),'数列3':(3,3,3,3),'数列4':(4,4,4,4)})
a.index={'行1','行2','行3','行4'}
#这里dataframe类型a就是要输出的表格
sub="test"
d=''
#表格内容
for i in range(len(a)):
d=d+"""
""" + str(a.index[i]) + """ |
""" + str(a.iloc[i][0]) + """ |
""" + str(a.iloc[i][1]) + """ |
""" + str(a.iloc[i][2]) + """ |
""" + str(a.iloc[i][3]) + """ |
"""
html = """\
测试程序邮件发送:
统计 |
数列1 |
数列2 |
数列3 |
数列4 |
"""+d+"""