2011-11-29 1 views
3

je développer une fonction qui envoie des e-mails PYTHON avec une pièce jointe .doc et une pièce jointe d'image (.jpg)afficher une image attachée sur le message HTML

Le message de l'e-mail est HTML. Je veux savoir comment afficher l'image attachée sur le message HTML .... y a-t-il une instruction qui peut m'aider avec ceci???

Merci beaucoup .... Voici la fonction que j'ai développé

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 

Répondre

11

Dans votre code html, utilisez les balises img src avec un de:

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

Ainsi, par exemple:

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

Merci l'homme ..... cela a très bien fonctionné! – mauguerra

+1

Pas de problème. Accepter ma réponse serait bien sûr apprécié. :-) –