2011-11-29 18 views
3

予有發展PYTHON一個函數與一個.DOC附着和一個圖像附件(.JPG)上的HTML消息

電子郵件的消息是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 

回答

11

在你的HTML功能,使用的img標籤與一個src:

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

所以,舉例來說:

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

感謝男人.....它工作得很好!!! – mauguerra

+1

沒問題。接受我的答案當然會被讚賞。 :-) –