Django EmailMultiAlternatives with Embadded Image in HTML

313 views
Skip to first unread message

ars_sim

unread,
Jun 20, 2011, 5:06:40 PM6/20/11
to Django users
I am trying to send email with HTML and embadded image. Email sending
working fine and its also attaching the image in email but its not
showing the image in HTML (inline). I used Content:ID in view and cid
in template but no success :(. I explore so many forms and apply
solutions but in vain please help!. Here is my code:

html_content = render_to_string('newsletters/
newsletter_template.html', vars)
text_content = strip_tags(html_content)

to = 'mye...@gmail.com'


msg = EmailMultiAlternatives(self.subject, text_content,
settings.STAFF_FROM_EMAIL, [to])
msg.attach_alternative(html_content, "text/html")

image_file = open('../media/images/banner_admin.gif', 'rb')
msg_image = MIMEImage(image_file.read())
image_file.close()

msg_image.add_header('Content-ID', '<image1>')
msg.attach(msg_image)

msg.send()

Template:

<div style="border:2px solid #CCCCCC; width:900px; font-size:10px;
padding:5px;">
<div style="margin-bottom: 10px;"><img src="cid:image1" /></div>
<div style="">{{body|linebreaks}}</div>
</div>

Kevin Renskers

unread,
Jun 22, 2011, 4:25:47 AM6/22/11
to django...@googlegroups.com
If I google on "django html email inline image" I see multiple solutions. None of them worked? Seems unlikely to me...
Reply all
Reply to author
Forward
0 new messages