Display an attached image on the HTML message

心已入冬 提交于 2019-12-20 15:23:08

问题


I have develop a function in PYTHON that sends emails with one .doc attachment and one image attachment (.jpg)

The message of the email is HTML.

I want to know how to display the Attached Image on the HTML message....is there any instruction that can help me with this???

Thanks a lot.... Here is the function I developed

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

回答1:


In your html, use img tags with a src of:

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

So, for instance:

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


来源:https://stackoverflow.com/questions/8304618/display-an-attached-image-on-the-html-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!