python 发送html 邮件

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from datetime import timedelta, datetime
import os
import time

def send_email(bodyHtml):
    fromaddr = 'abc@163.com'
    password = 'x'
    toaddrs = ['abc@163.com','ziqiiii@qq.com']

    msgRoot = MIMEMultipart('daily_data')
    msgRoot['From'] = fromaddr
    msgRoot['To'] = ', '.join(toaddrs)

    update_format = datetime.datetime.now().strftime("%Y-%m-%d") #日期
    subject = update_format + ' 数据对比'
    msgRoot['Subject'] = Header(subject, 'utf-8')

    msgAlternative = MIMEMultipart('alternative')
    msgRoot.attach(msgAlternative)

    msgAlternative.attach(MIMEText(bodyHtml, 'html', 'utf-8'))
    try:
        server = smtplib.SMTP('smtp.163.com')
        server.login(fromaddr,password)
        server.sendmail(fromaddr, toaddrs, msgRoot.as_string())
        print('success')
    except smtplib.SMTPException, e:
        print('error:',e) #打印错误
    finally:
    	server.quit()

 

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页