如何使用python将html文件内容打印为电子邮件的正文?

时间:2016-01-27 13:06:28

标签: python html python-2.7 email smtp

matches = []
for root, dirnames, filenames in os.walk('C:\Users\top\UDI\New folder'):
    for filename in fnmatch.filter(filenames, '*.html'):
        matches.append(os.path.join(root, filename))
        filename = root.rstrip(os.sep) + os.sep+ filename
        #print filename

        fromaddress = 'ucm_embed_test'
        toaddress = "hetappa@ace.com, hepa@gmail.com"
        text = "Test is Parsed"

        msg = MIMEMultipart('alternative')
        msg['From'] =fromaddress
        msg['To'] = toaddress
        msg['Subject'] = text

        print "Sending mail"
        ctype, encoding = mimetypes.guess_type(filename)
        if ctype is None or encoding is not None:
            ctype = "application/octet-stream"
        maintype, subtype = ctype.split("/", 1)

        with open(filename) as fp:
            attachment = MIMEText(fp.read(), _subtype=subtype)
        attachment.add_header("Content-Disposition", "attachment",\
                              filename=os.path.basename(filename))

        msg.attach(MIMEText(open(filename).read(), "text/html"))
        server = smtplib.SMTP('eu-smtp.nuae.com')
        server.ehlo()
        #server.starttls()
        #server.login(username,password)
        server.sendmail(fromaddress,toaddress.split(','),msg.as_string())
        server.quit()

我正在阅读.html文件,然后将其作为电子邮件发送给相应的人。但是电子邮件中出现的邮件正文是以html标签的形式出现的。如何将我的答案表示为邮件正文中的字符。有人可以帮助我吗?

0 个答案:

没有答案