2、发送邮件,并添加附件
file_path = 'file.xls' ws.save(file_path)
connection = mail.get_connection() connection.open() email1 = mail.EmailMessage( '测试,谢谢!', '没有内容', '发件人@com.cn', ['收件人邮箱'], connection=connection, ) text = open(file_path, 'rb').read() file_name = os.path.basename(file_path) b = make_header([(file_name, 'utf-8')]).encode('utf-8') email1.attach(b,text) email1.send() connection.close()效果:
注意在部署环境下,file_path需要写入绝对路径:
file_path = 'D:/wwwroot/Apache24/htdocs/file/PFMEA_' + title +'_RPL=1.xls'