Login

Very simple email image embed

Author:
ssadler
Posted:
May 15, 2009
Language:
Python
Version:
1.0
Score:
2 (after 2 ratings)

This is a simple way to embed images in emails, rather than use absolute links, which many clients will not show by default. It has not undergone extensive testing but it should get you started. Comments / suggestions welcome.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from email.MIMEImage import MIMEImage

def email_embed_image(email, img_content_id, img_data):
    """
    email is a django.core.mail.EmailMessage object
    """
    img = MIMEImage(img_data)
    img.add_header('Content-ID', '<%s>' % img_content_id)
    img.add_header('Content-Disposition', 'inline')
    email.attach(img)

More like this

  1. Browser-native date input field by kytta 1 month, 1 week ago
  2. Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
  3. My firs Snippets by GutemaG 1 month, 2 weeks ago
  4. FileField having auto upload_to path by junaidmgithub 2 months, 3 weeks ago
  5. LazyPrimaryKeyRelatedField by LLyaudet 3 months ago

Comments

ssadler (on May 15, 2009):

Example usage: from django.core.mail import EmailMessage from myproject import email_embed_image

img_content_id = 'main_image'
img_data = ''
open('/path/to/image', 'rb').read()
body = '<img src="cid:%s" />' % img_content_id

email = EmailMessage('title', body, '[email protected]', ['[email protected]'])
email_embed_image(email, img_content_id, img_data)
email.send()

#

Tarken (on September 10, 2009):

This is exactly what I needed, thank you!

#

Please login first before commenting.