Email Plaintext and HTML Body via Template


I would like to send formdatas via email to users as plaintext and html. I use plone.api with the following statements to attach plain and html.

Simple Example:

formdatas = {"val1":"blub", "val2":"blob"}
text = "Hello World \nI'm John Doe\n"
html = "<h2>Hello World<br/>I'm John Doe</h2>"
for key, value in formdatas.iteritems():
    text += key + " : " + value + "\n\n"
    html += "<p>" + key + " : " + value + "</p>"

What is the best implementation to render HTML?
is there a possibility via Products.Five.browser.pagetemplatefile.ViewPageTemplateFile
or a other mechanism?

html =

message = MIMEMultipart()
part1 = MIMEText(text.encode('utf-8'), 'plain', 'utf-8')
part2 = MIMEText(html, 'html')
    sender = sender,

I would like generate the html in a template, it is easier/nicer to design the email and change the HTML-Snippet. Should i register a View like described in the docs ? It's not clear for me what's the right way. Can anyone help?

We store email templates in general on the filesystem inside some folder.

Here is a gist of a template and some related code for formatting the templates and sending them.

It uses the standard Python string interpolation mechanism.
It can be used for formatting both plain text messages and HTML messages. Deriving HTML from plain text or vice versa is unlikely to work. So you need a plain text and an HTML template for one email to be send (usually).


1 Like

andreas is right - in most cases creating the plain text out of the html (or the other way round) does not lead to optimal results.

however, singing&dancing (collective.dancing and collective.singing) are doing it this way:

s&d is using the formatter.AbstractFormatter for transforming html to txt.
one can also use beautifulsoup for this.

we recently used html2text which creates markdown syntax for a given html file (see the gist below)
personally i liked the result much more than abstractformatter (especially the link handling)


Link Text[1]


# this also breaks links like the following because the appened footnote-number break the get parameter (at least in some email clients)


[Link Text](<>)