Python
基本用法
列表与字典
选择与循环语句
函数与模块
中文转拼音
Excel操作
Panads
DOC操作
调用etcd模块
调用zookeeper模块
调用redis模块
pyinstaller打包
安装conda
使用conda打包32程序
python发送html邮件
HTTP请求
自建代理ip池
mysql操作
Python图片生成
HTML转图片
文件md5及base64
调用mongo
多进程与多线程
pyecharts绘图模块
多环境管理(Pyenv/Virtualenv)
PIP
本文档使用MrDoc发布
返回首页
-
+
python发送html邮件
2020年7月5日 15:52
admin
#PYTHON2 # coding=utf-8 import smtplib from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from email.header import Header with open("./maoli_info.txt", "r") as f: dx_txt = f.read() f.close() # 发送邮件 def send_Email(): # 邮件接受者 mail_receiver = ['wujiaqiang@wxchina.com','wja84281865@163.com'] #mail_receiver = ['wujiaqiang@wxchina.com'] # 根据不同邮箱配置 host,user,和pwd mail_host = 'smtp.exmail.qq.com:465' mail_user = 'cpaas@wxchina.com' mail_pwd = '密码' mail_to = ','.join(mail_receiver) msg = MIMEMultipart() environsString = dx_txt message = environsString body = MIMEText(message, _subtype='html', _charset='utf-8') msg.attach(body) msg['To'] = mail_to msg['from'] = mail_user msg['subject'] = '毛利率告警' #构造附件1 att1 = MIMEText(open('/home/fan/yue/new/replace_supplier_merge_gbk.csv', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 # 附件名称为中文时的写法 att1.add_header("Content-Disposition", "attachment", filename = Header('账号关停.xlsx','UTF-8').encode()) # 附件名称非中文时的写法 att1["Content-Disposition"] = 'attachment; filename="账号关停.xlsx" msg.attach(att1) #构造附件2 att1 = MIMEText(open('/home/fan/yue/new/total_supplier_gbk.csv', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = 'attachment; filename="供应商余额明细.csv"' msg.attach(att1) try: # s = smtplib.SMTP() s = smtplib.SMTP_SSL() s.connect(mail_host) # s.starttls() s.login(mail_user, mail_pwd) s.sendmail(mail_user, mail_receiver, msg.as_string()) s.close() print 'success' except Exception, e: print e if __name__ == "__main__": send_Email() #PYTHON3 # coding=utf-8 import smtplib from email.header import Header from email.mime.text import MIMEText # 读取html文件内容 with open('G:\xxx\Python脚本\数据发送表格邮件\python3\maoli_info.txt', 'rb') as f: # HTML文件默认和当前文件在同一路径下,若不在同一路径下,需要指定要发送的HTML文件的路径 mail_body = f.read() #发件人和收件人 sender = 'cpaas@wxchina.com' receiver = ['wujiaqiang@wxchina.com','wja84281865@163.com'] # 所使用的用来发送邮件的SMTP服务器 smtpserver = 'smtp.exmail.qq.com' # 发送邮箱的用户名和授权码(不是登录邮箱的密码) username = 'cpaas@wxchina.com' password = '密码' # 邮件主题 mail_title = '接通率告警' # 邮件内容, 格式, 编码 message = MIMEText(mail_body, 'html', 'utf-8') message['From'] = sender message['To'] = Header(','.join(receiver)) message['Subject'] = Header(mail_title, 'utf-8') #构造附件1 att1 = MIMEText(open('/home/fan/yue/new/replace_supplier_merge_gbk.csv', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 附件名称为中文时的写法 att1.add_header("Content-Disposition", "attachment", filename = Header('账号关停.xlsx','UTF-8').encode()) # 附件名称非中文时的写法 att1["Content-Disposition"] = 'attachment; filename="账号关停.xlsx" message.attach(att1) #构造附件2 att1 = MIMEText(open('/home/fan/yue/new/total_supplier_gbk.csv', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = 'attachment; filename="供应商余额明细.csv"' message.attach(att1) try: smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.login(username, password) smtp.sendmail(sender, receiver, message.as_string()) print("发送邮件成功!!!") smtp.quit() except smtplib.SMTPException: print("发送邮件失败!!!") #参考资料 https://blog.csdn.net/qq_32641659/article/details/89296265
分享到: