python email 发送纯文本 html 和附件

0
python 发送一个包含纯文本、html和附件邮件的问题:
发送成功少纯文本的内容,代码没有报错,把其他的代码注掉仅发送纯文本内容,纯文本中的内容在邮件中是能看到的。
用的是email.mime.multipart()这个方法
具体代码见附件!

42dbf6152847702e6765c8b9d9d05037.png

 
已邀请:
0

qianqian062088 2017-02-17 回答

     # Send email
    me             = mail_from
    mailDate       = Header(time.ctime())
    msg            = MIMEMultipart()
    msg['Subject'] = Header(sub, 'utf-8')
    msg['date']    = mailDate
    msg['From']    = me
    msg['To']      = ";".join(mail_to_list)

    #mail content
    part = MIMEText(content,_subtype='plain',_charset='utf-8')
    msg.attach(part)

    #attached file
    part = MIMEApplication(open(logname,'rb').read())
    filename=datetime.date.today().strftime("%Y%m%d")+'_dwh.log'
    part.add_header('Content-Disposition', 'attachment', filename=filename)
    msg.attach(part)

    try:
        server = smtplib.SMTP()
        server.connect(mail_host, mail_port)
        server.login(mail_from,mail_pass)
        server.sendmail(me, mail_to_list, msg.as_string())
        server.close()
    except Exception as err:
        log.error(err)  
        raise err

要回复问题请先登录注册