Login

send_html_mail

Author:
amitu
Posted:
October 14, 2008
Language:
Python
Version:
1.0
Tags:
html-mail
Score:
3 (after 3 ratings)

There are many versions, this is the one I like. This is quite generic, can auto generate text version of the mail if required.

 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
# send_html_mail # {{{
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from smtplib import SMTP
import email.Charset

from django.conf import settings

charset='utf-8'

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

def send_html_mail(
    subject, sender="no-reply@example.com", recip="", context=None, 
    html_template="", text_template="", sender_name="",
    html_content="", text_content="", recip_list=None, sender_formatted=""
):
    if not context: context = {}
    if html_template:
        html = render(context, html_template)
    else: html = html_content
    if text_template:
        text = render(context, text_template)
    else: text = text_content
    if not text:
        text = html2text(_sanitizeHTML(html,charset))        

    if not recip_list: recip_list = []
    if recip: recip_list.append(recip)

    try:
        server = SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
        if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
            server.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
    except Exception, e: 
        print e
        return
    
    if not sender_formatted:
        sender_formatted = "%s <%s>" % (sender_name, sender) 

    for recip in recip_list:
        msgRoot = MIMEMultipart('related')
        msgRoot['Subject'] = subject.encode("utf8", 'xmlcharrefreplace')
        msgRoot['From'] = sender_formatted.encode("utf8", 'xmlcharrefreplace')
        msgRoot['To'] = recip.encode("utf8", 'xmlcharrefreplace')
        msgRoot.preamble = 'This is a multi-part message in MIME format.'

        msgAlternative = MIMEMultipart('alternative')
        msgRoot.attach(msgAlternative)

        msgAlternative.attach(MIMEText(smart_str(text), _charset=charset))
        msgAlternative.attach(MIMEText(smart_str(html), 'html', _charset=charset))

        try:
            server.sendmail(sender, recip, msgRoot.as_string())
        except Exception, e: print e

    server.quit()


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

# }}}

More like this

  1. django asynchronous send mail by gchandrasa 4 years, 11 months ago
  2. Better debugging mail server by yourcelf 4 years, 1 month ago
  3. Import mail into a Django model by pbx 7 years, 11 months ago
  4. Multiple emails form field by virhilo 5 years ago
  5. One line SMTP sink server by Baguage 8 months ago

Comments

Please login first before commenting.