Login

Sending html emails with images using Django templates

Author:
sleytr
Posted:
June 14, 2007
Language:
Python
Version:
.96
Score:
1 (after 1 ratings)

I have not extensively test this yet. But working for templates with embedded images.

If you want to use Django template system use msg and optionally textmsg as template context (dict) and define template and optionally texttemplate variables. Otherwise msg and textmsg variables are used as html and text message sources.

If you want to use images in html message, define physical paths and ids in tuples.
(image paths are relative to MEDIA_ROOT)

example: images=(('email_images/logo.gif','img1'),('email_images/footer.gif','img2')) use them in html like this: <img src="cid:img1"><br><img src="cid:img2">

stripogram and feedparser modules are used for extract plain text from html message source. If you are going to define text partition explicitly, than you can comment out line 10,11 and 48.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-

# Sending html emails in Django
# Report any bugs to esat @t sleytr*net
# Evren Esat Ozkan


#download and install feedparser from http://feedparser.org
#download and install StripOGram from http://www.zope.org/Members/chrisw/StripOGram
from feedparser import _sanitizeHTML
from stripogram import html2text

from django.conf import settings
from django.template import loader, Context

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from smtplib import SMTP
import email.Charset


charset='utf-8'
smtp_server='localhost'
smtp_user=''
smtp_pass=''

email.Charset.add_charset( charset, email.Charset.SHORTEST, None, None )

def htmlmail(sbj,recip,msg,template='',texttemplate='',textmsg='',images=(), recip_name='',sender=settings.DEFAULT_FROM_EMAIL,sender_name='',charset=charset):
   '''
   if you want to use Django template system:
      use `msg` and optionally `textmsg` as template context (dict)
      and define `template` and optionally `texttemplate` variables.
   otherwise msg and textmsg variables are used as html and text message sources.
   
   if you want to use images in html message, define physical paths and ids in tuples.
   (image paths are relative to  MEDIA_ROOT)
   example: 
   images=(('email_images/logo.gif','img1'),('email_images/footer.gif','img2'))
   and use them in html like this:
   <img src="cid:img1">
   ...
   <img src="cid:img2">
   '''
   html=render(msg,template)
   if texttemplate or textmsg: text=render((textmsg or msg),texttemplate)
   else: text= html2text(_sanitizeHTML(html,charset))

   msgRoot = MIMEMultipart('related')
   msgRoot['Subject'] = sbj
   msgRoot['From'] = named(sender,sender_name)
   msgRoot['To'] =  named(recip,recip_name)
   msgRoot.preamble = 'This is a multi-part message in MIME format.'

   msgAlternative = MIMEMultipart('alternative')
   msgRoot.attach(msgAlternative)
   
   msgAlternative.attach(MIMEText(text, _charset=charset))
   msgAlternative.attach(MIMEText(html, 'html', _charset=charset))

   for img in images:
      fp = open(settings.MEDIA_ROOT+img[0], 'rb')
      msgImage = MIMEImage(fp.read())
      fp.close()
      msgImage.add_header('Content-ID', '<'+img[1]+'>')
      msgRoot.attach(msgImage)

   smtp = SMTP()
   smtp.connect(smtp_server)
   if smtp_user: smtp.login(smtp_user, smtp_pass)
   smtp.sendmail(sender, recip, msgRoot.as_string())
   smtp.quit()


def render(context,template):
   if template:
      t = loader.get_template(template)
      return t.render(Context(context))
   return context

def named(mail,name):
   if name: return '%s <%s>' % (name,mail)
   return mail

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

sleytr (on June 14, 2007):

Thank you. I corrected the code.

#

kwast (on April 15, 2009):

Hi, your snippet save me but...

I had some issues with accentuated chars in my templates. So I did a small workaround to solve this.

msgAlternative.attach(MIMEText(html.encode('ascii','xmlcharrefreplace'), 'html', _charset=charset))

I don't know if other people had this issue, I'm using Python 2.5 and I speak Brazilian Portuguese, so I have 99% of chance that at least one word in my template have an accentuated char.

Thank you very much

#

alexhayes (on November 7, 2013):

Please see https://djangosnippets.org/snippets/3001/ for a modification to this code that supports MIMEBase/MIMEImage properly.

#

Please login first before commenting.