mau*_*rra 0 html python sendmail mime-types mime-message
我正在用Python编程.我已经有一个函数发送带有消息和附件的电子邮件....我唯一的问题是我希望消息是HTML,但我的不尊重.....
这是我正在使用的功能
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
Run Code Online (Sandbox Code Playgroud)
我希望你能告诉我要改变什么或添加什么,所以我发送的消息可能是HTML ....
我正在使用"MIXED"Multipart,因为HTML消息将包含一些不会附加但会成为消息的一部分的图像.....
更换
msg.attach(MIMEText(text))
Run Code Online (Sandbox Code Playgroud)
通过
msg.attach(MIMEText(text, 'html'))
Run Code Online (Sandbox Code Playgroud)
(默认为'普通')