Sending HTML emails in Zope

26 October 2006   3 comments   Zope

Mind That Age!

This blog post is 12 years old! Most likely, its content is outdated. Especially if it's technical.

Here are a few lessons learnt when sending HTML emails in Zope with unicode data. I'm still a beginner with this stuff and still probably have a lot to learn. What I've achived now works but might break with different encodings or on different systems so don't assume that these patterns will work in your setup.

To the send the email I'm using the default MailHost that comes with Zope 2.8.5. I call it's send() passing the subject line a second time because the subject line is already in the body string.

The most valuable piece of magic to learn and remember from this is that when you construct the multi-part message you have to attach the plain text before the html text. Thank you Lukasz Lakomy for that tip!

Here's the code that constructs the message and send it:

# -*- coding: iso-8859-1 -*-
e_subject = u'Provsprängning'
e_from = u'some@spammer.com'
e_to = u'some@receiver.com'
body_html = u'<html>provsprängningen <em>av</em> kärnvapen</html>'
body_plain = u'provsprängningen *av* kärnvapen'

mime_msg = MIMEMultipart('related')
mime_msg['Subject'] = e_subject
mime_msg['From'] = e_from
mime_msg['To'] = e_to
mime_msg.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body 
# in an 'alternative' part, so message agents can decide 
# which they want to display.
msgAlternative = MIMEMultipart('alternative')
mime_msg.attach(msgAlternative)

# plain part
msg_txt = MIMEText(body,  _charset='iso-8859-1')
msgAlternative.attach(msg_txt)

# html part
msg_txt = MIMEText(rendered_html, _subtype='html', 
                   _charset='iso-8859-1')
msgAlternative.attach(msg_txt)

self.MailHost.send(mime_msg.as_string())

Comments

Lukasz

You welcome PeterBe :D

minskmaz

#working version as external method
# -*- coding: iso-8859-1 -*-

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

def htmlMailer(self):
e_subject = u'Sent to you through plone'
e_from = u'some@spammer.com'
e_to = u'some@receiver.com'
body_html = """<html>provsprängningen <em>av</em> kärnvapen</html>"""
body_plain = u'provsprängningen *av* kärnvapen'

mime_msg = MIMEMultipart('related')
mime_msg['Subject'] = e_subject
mime_msg['From'] = e_from
mime_msg['To'] = e_to
mime_msg.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body
# in an 'alternative' part, so message agents can decide
# which they want to display.
msgAlternative = MIMEMultipart('alternative')
mime_msg.attach(msgAlternative)

# plain part
msg_txt = MIMEText(body_plain, _charset='iso-8859-1')
msgAlternative.attach(msg_txt)

# html part
msg_txt = MIMEText(body_html, _subtype='html',
_charset='iso-8859-1')
msgAlternative.attach(msg_txt)

self.MailHost.send(mime_msg.as_string())

imran

as_string() saved my life! Thanks alot!

Your email will never ever be published


Related posts

Previous:
Catching a carriage return in bash 23 October 2006
Next:
Oxfordtube's website sucks 30 October 2006
Related by Keyword:
Premailer.io 08 July 2015
How to slice a rune in Go 16 March 2015
html2plaintext Python script to convert HTML emails to plain text 10 August 2007
is is not the same as equal in Python 01 December 2006
Entering multilanguage data in Zope 26 November 2006
Related by Text:
jQuery and Highslide JS 08 January 2008
I'm back! Peterbe.com has been renewed 05 June 2005
Anti-McCain propaganda videos 12 August 2008
Ever wondered how much $87 Billion is? 04 November 2003
Guake, not Yakuake or Yeahconsole 23 January 2010