带HTML消息的sendmail [英] sendmail with HTML message

查看:120
本文介绍了带HTML消息的sendmail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python编程.我已经有一个发送带有消息和附件的电子邮件的功能....我唯一的问题是我希望消息为HTML,但是我不尊重.....

I am programming with Python. I already have a function that sends an email with a message and an attachment....My only problem is that I want the message to be HTML, but mine does not respect that.....

这是我正在使用的功能

def enviarCorreo(fromaddr, toaddr, text, file):
   msg = MIMEMultipart('mixed')
   msg['From'] = fromaddr
   msg['To'] = toaddr
   msg['Subject'] = 'asunto'
   msg.attach(MIMEText(text))
   #adjunto
   adjunto = MIMEBase('application', "octet-stream")
   adjunto.set_payload(open(file, "rb").read())
   encode_base64(adjunto)
   anexo = os.path.basename(file)
   adjunto.add_header('Content-Disposition', 'attachment; filename= "%s"' % anexo)
   msg.attach(adjunto)
   #enviar
   server = smtplib.SMTP('localhost')
   server.set_debuglevel(1)
   server.sendmail(fromaddr, toaddr, msg.as_string())
   server.quit()
   return

希望您能告诉我要更改或添加的内容,以便我发送的消息可以是HTML....

I hope you can tell me what to change or what to add so the message I send could be HTML....

我正在使用"MIXED"多部分,因为HTML消息将包含一些图像,这些图像不会附加但会成为消息的一部分.....

I am using the "MIXED" Multipart because the HTML message will contain some images that would not be attached but would be part of the message.....

推荐答案

替换

msg.attach(MIMEText(text))

作者

msg.attach(MIMEText(text, 'html'))

(默认为普通")

这篇关于带HTML消息的sendmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆