views:

175

answers:

2

Hi, I'm using the EmailMultiAlternatives class to send text and html mails with django. While testing with some dummy code, I wanted to add an image with some text.

msg = EmailMultiAlternatives('My subject','some text here', '[email protected]', ['to@my_domain.com'])
msg.attach_alternative('<p>here is what I was talking about</p> <img src="logo.png" alt="logo_here" /> <div>You see???</div>', 'text/html')
msg.attach_file('/var/my_site/static/images/logo.png')
msg.send()

The problem is that on the email client the image is not displaying...

Looking at the raw email, I found this:

--===============1013820581535380480==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable

<p>here is what I was talking about</p> <img src=3D"logo.png" alt=3D"logo_h=
ere" /> <div>You see???</div>
--===============1013820581535380480==--

Does anybody have an idea of what I'm doing wrong??

Thanks!

Edit: I could manage to embed an image into the html mail. It seems that the EmailMultiAlternatives has an attach method that can accept an MimeImage object. Actually it can accept anything that inherits from MimeBase.

fp = open('test.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

mimeImage = MimeImage(fp.read())
mimeImage.add_header('Content-ID', '<logo.png>')
msg.attach(mimeImage)
msg.send()
+1  A: 

This is only a partial solution. When I create an email in thunderbird and embed an image (it's visible when I view the message), the source looks like this:

--------------070800070205000904000708
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
<img alt="asdsa" src="cid:[email protected]"
 height="38" width="150"><br>
</body>   
</html>

--------------070800070205000904000708
Content-Type: image/jpeg;
 name="added.jpg"
Content-Transfer-Encoding: base64
Content-ID: <[email protected]>
Content-Disposition: inline;
 filename="added.jpg"

then the contents of the image follow. The URL in the image tag needs to be the Content-ID of the attached image. I'm just not sure how to specify that.

EDIT: It seems you can do it with the email module in the standard library instead of Django's EmailMultiAlternatives. See here: http://code.activestate.com/recipes/473810/

Gabriel Ross
A: 

I had the same problem and here is what i found:

First, a disclaimer. I don't know much about email message standards. This is what i found after searching,reading and experimenting. To my knowledge, it works.

1) The problem with images not displaying is because the EmailMessage (and EmailMultiAlternatives) classes in Django use "multipart/mixed" content type for the message, when in fact you need "multipart/related". What i did was:

msg = EmailMultiAlternatives()
msg.mixed_subtype = 'relative'

Thats it!

2) The problem with 3D appearing should not affect your message. I think it is part of the 'quoted printable' spec. If you don't like it and you prefer a 8bit or 7bit try this at the top of your file:

from email import Charset
# Due to http://code.djangoproject.com/ticket/11212
Charset.add_charset('utf-8',Charset.SHORTEST,None,'utf-8')

As the comment suggets, i got this from that django ticket.

I know this question is rather old, but there was no reply that satisfied me and was the only one that turned up on a google search on this issue.

arcanum