-
-
Notifications
You must be signed in to change notification settings - Fork 117
Open
Labels
Description
Moving this bit from PR #742:
def send_by_template(self, template, data={}, **kw):
# test if template exists by loading it
def _template_exists(template):
# check if stored in cache already
if template in self._template_exists_cache:
return self._template_exists_cache[template]
# do first time check from file or module
result = False
try:
# successfully load when available
self.app.template.load(template)
result = True
except:
pass
# store flag in cache list to prevent often load access
self._template_exists_cache[template] = result
# return state
return result
# prepare email params
params = dict(**kw)
# check render subject
if 'subject' not in params:
if _template_exists(f'{template}.title.jinja2'):
params['subject'] = self.app.render(data,
f'{template}.title.jinja2',
out=None)
# build body
body = list()
if _template_exists(f'{template}.plain.jinja2'):
body.append(self.app.render(dict(**data, mail_params=params),
f'{template}.plain.jinja2',
out=None))
if _template_exists(f'{template}.html.jinja2'):
# before adding a html part make sure that plain part exists
if len(body) == 0:
body.append('Content is delivered as HTML only.')
body.append(self.app.render(dict(**data, mail_params=params),
f'{template}.html.jinja2',
out=None))
# send the message
self.send(body=body, **params)Would be convenient to be able to link templates with sending mail, however it needs to be TemplateHandler agnostic.