Display embedded images on iPhone, iPad

I am trying to create a letter in Django with embedded 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 call it like this:

 <img src="cid:image1" /> 

This works great in web browsers, Outlook, Thunderbird ... everything except Apple's email client on OSX, iPad and iPhone. Images are displayed twice. They are placed correctly, but they are also attached to the bottom of the letter. My question is: how do I get rid of the images below? or should I approach images in emails in different ways.

Literature:
http://djangosnippets.org/snippets/1507/
Django: how to send HTML messages with embedded images
creating a MIME email template with images to send using python / django

+7
source share
1 answer

Different email clients choose to render multipart/mixed messages differently.

Most customers prefer to display each part (in the "multipart" message) inline - in the order in which they were added to the email. However, if the image is mentioned in the text/html , most clients do not display this image later later as part of the "nesting all parts" process.

Apple Mail differs on OSX and iOS because they will display each part of the multipart/mixed message in the order in which they were included, regardless of any internal links between HTML and images. This will cause your images to appear once in your HTML document and again at the end of the message where they were automatically added.

The solution is to group your HTML and image files into one related part. i.e:.

 from django.core.mail import EmailMultiAlternatives from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # HTML + image container related = MIMEMultipart("related") # Add the HTML html = MIMEText('an image: <img src="cid:some_image"/>', "html") related.attach(html) # Add an image with open("icon.png", "rb") as handle: image = MIMEImage(handle.read()) image.add_header("Content-ID", "<some_image>") image.add_header("Content-Disposition", "inline") related.attach(image) # top level container, defines plain text version email = EmailMultiAlternatives(subject="demo", body="plain text body", from_email=" foo@example.com ", to=[" bar@example.com "]) # add the HTML version email.attach(related) # Indicate that only one of the two types (text vs html) should be rendered email.mixed_subtype = "alternative" email.send() 
+10
source

All Articles