zoukankan      html  css  js  c++  java
  • python3发送html格式的邮件

    def send_mail(to_list, sub, content, attpath):
        me = "*******" + "<" + mail_user + "@" + mail_postfix + ">"
        msg = MIMEMultipart()
        msg['Subject'] = sub
        msg['From'] = me
        msg['To'] = ";".join(to_list)
    
        htmlf=open(attpath,'r',encoding="utf-8")
        htmlcont=htmlf.read()
    
        email_text = MIMEText(htmlcont, _subtype='html')
        msg.attach(email_text)
        sep = os.sep
        attname = attpath.split(sep)[-1]
        # 首先是xlsx类型的附件
        email_att = MIMEApplication(open(attpath, 'rb').read())
        email_att.add_header('Content-Disposition', 'attachment', filename=attname)
        msg.attach(email_att)
        try:
            server = smtplib.SMTP()
            server.connect(mail_host)  # 连接服务器
            server.login(mail_user, mail_pass)  # 登录操作
            server.sendmail(me, to_list, msg.as_string())
            server.close()
            return True
        except BaseException:
            return False

    关键是加入

    _subtype='html',如果是普通文本,就是plain,
  • 相关阅读:
    hdu 4081 Qin Shi Huang's National Road System
    Finding Team Member
    hdu 5491 The Next
    Queue
    Backward Digit Sums
    HDU
    HDU
    CodeForces 500 A. New Year Transportation
    拓扑排序
    “玲珑杯”ACM比赛 Round #1 题解
  • 原文地址:https://www.cnblogs.com/xqnq2007/p/8204975.html
Copyright © 2011-2022 走看看