在HTML邮件上显示附加图像 [英] Display an attached image on the HTML message

查看:932
本文介绍了在HTML邮件上显示附加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PYTHON中开发了一个带有一个.doc附件和一个图片附件(.jpg)的电子邮件功能。

电子邮件的消息是HTML。 / p>

我想知道如何在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.a dd_header('Content-Disposition','附件;文件名=%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


解决方案

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

  cid:< Content-Disposition标题中使用的文件名> 

例如:

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


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

解决方案

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>

这篇关于在HTML邮件上显示附加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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