I’m trying to create an email in Django with inline images.
msg = EmailMultiAlternatives(...)
image_file = open('file_path', 'rb')
img = MIMEImage(img_data)
image_file.close()
img.add_header('Content-ID', '<image1>')
img.add_header('Content-Disposition', 'inline')
msg.attach(img)
msg.send()
And in the template I would reference it like so:
<img src="cid:image1" />
This works fine in web browsers, outlook, thunderbird … all except for the apple mail client on OSX, iPad and iPhone. The images are displayed twice. They are placed inline correctly but they are also attached to the bottom of the email. My question is, how do I get rid of the images at the bottom? or should I approach images in emails differently.
References:
http://djangosnippets.org/snippets/1507/
Django: How to send HTML emails with embedded images
creating a MIME email template with images to send with python / django
Different email clients choose to render
multipart/mixedmessages in different ways.Most clients choose to render each part (in a “multipart” message) inline – in the order they were added to the email. However, if an image is referred to in a
text/htmlpart, most clients don’t display that image again later on as part of the “inlining all parts” process.Apple Mail on OSX and iOS are different, in so far as they will display each part in a
multipart/mixedmessage in the order they were included, regardless of any inner references between HTML and images. This results in your images being displayed once within your HTML, and again at the end of the message where they’ve been inlined automatically.The solution is to group your HTML and image assets into a single
relatedpart. i.e.: