lavr / python-emails

Modern python library for emails
http://python-emails.readthedocs.org
Other
400 stars 54 forks source link

Import jinja template from external file #74

Closed dalepotter closed 7 years ago

dalepotter commented 7 years ago

Hi, nice library!

Is it possible to import a template from an external file (for example template.html)? I've adapted the example code in the docs, but am getting a TypeError - see below.

If this is possible, I'm happy to send a pull request to extend the docs examples.

sendemail.py

import emails
from emails.template import JinjaTemplate as T

message = emails.html(
     subject=T('Payment Receipt No.{{ billno }}'),
     html=T(open('template.html')),
     mail_from=('ABC', 'robot@mycompany.com')
     )

r = message.send(...)

This gives:

$ python sendemail.py
Traceback (most recent call last):
... 
TypeError: Can't compile non template nodes
lavr commented 7 years ago

Hi!

JinjaTemplate constructor receives unicode string as first argument, so try T(open('template.html').read())

For non-ascii content you should probably decode text to unicode: T(open('template.html').read().decode('latin-1'))