【问题标题】:Display an attached image on the HTML message在 HTML 消息上显示附加图像
【发布时间】:2012-01-08 10:19:20
【问题描述】:

我在 PYTHON 中开发了一个功能,可以发送带有一个 .doc 附件和一个图像附件 (.jpg) 的电子邮件

电子邮件的信息是 HTML。

我想知道如何在 HTML 消息上显示附加图像....有什么说明可以帮助我吗???

非常感谢.... 这是我开发的功能

def enviarCorreo(fromaddr, toaddr, text, file, imagen_1, imagen_2):
   msg = MIMEMultipart('mixed')
   msg['From'] = fromaddr
   msg['To'] = toaddr
   msg['Subject'] = 'asunto'
   msg.attach(MIMEText(text,'HTML'))

   #IMAGE ATTACHMENT *******************************************
   adjuntoImagen_1 = MIMEBase('application', "octet-stream")
   adjuntoImagen_1.set_payload(open(imagen_1, "rb").read())
   encode_base64(adjuntoImagen_1)
   anexoImagen_1 = os.path.basename(imagen_1)
   adjuntoImagen_1.add_header('Content-Disposition', 'attachment; filename= "%s"' % anexoImagen_1)
   msg.attach(adjuntoImagen_1)

   #FILE ATACHMENT **********************************************
   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)

   #SEND ********************************************************
   server = smtplib.SMTP('localhost')
   server.set_debuglevel(1)
   server.sendmail(fromaddr, toaddr, msg.as_string())
   server.quit()
   return

【问题讨论】:

    标签: python html mime-types sendmail mime-message


    【解决方案1】:

    在您的 html 中,使用带有 src 的 img 标签:

    cid:<filename used in your Content-Disposition header>
    

    例如:

    <p>
    <img src="cid:image1.jpeg"/>
    </p>
    

    【讨论】:

    • 没问题。接受我的回答当然不胜感激。 :-)
    猜你喜欢
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多