websauna.system.mail package¶
Sending out HTML and plain text email.
-
websauna.system.mail.
render_templated_mail
(request, template, context)[source]¶ Render an email that is divided to three template files.
The email is assembled from three different templates:
Read subject from a subject specific template $template.subject.txt
Generate HTML email from HTML template, $template.body.html
Generate plain text email from HTML template, $template.body.txt
Make sure you have configured your template engine (Jinja 2) to read TXT templates beside HTML.
HTML body goes through Premailer transform step, inlining any CSS styles.
- Parameters
template (
str
) – Template filename base string for template tripled (subject, HTML body, plain text body). For exampleemail/my_message
would map to templatesemail/my_message.subject.txt
,email/my_message.body.txt
,email/my_message.body.html
context (
dict
) – Template context dictionary passed to the rendererer
- Return type
- Returns
Tuple(subject, text_body, html_body)
-
websauna.system.mail.
send_templated_mail
(request, recipients, template, context, sender=None, immediate=None, tm=None)[source]¶ Send out templatized HTML and plain text emails.
Each HTML email should have a plain text fallback. Premailer package is used to convert any CSS styles in HTML email messages to inline, so that email clients display them.
The email is assembled from three different templates:
Read subject from a subject specific template $template.subject.txt
Generate HTML email from HTML template, $template.body.html
Generate plain text email from HTML template, $template.body.txt
Make sure you have configured your template engine (Jinja 2) to read TXT templates beside HTML.
Example to send email with a custom From header, using
myapp/email/welcome.body.html
as the body template:from email.header import Header from email.utils import formataddr # I never receive any letters to_email = "[email protected]" # Build a proper From header with both name and email, # otherwise the sender defaults to the site INI settings sender = formataddr((str(Header(order.get_sender_name(), 'utf-8')), order.get_from_email())) # Pass some variables to subject and body templates context = dict(greeting="Welcome human!", tag_line="We come in peace") send_templated_mail(self.request, [to_email], "myapp/email/welcome", context=context, sender=sender)
- Parameters
request (
Request
) – HTTP request, passed to the template engine. Request configuration is used to get hold of the configured mailer.recipients (
List
) – List of recipient emailstemplate (
str
) – Template filename base string for template tripled (subject, HTML body, plain text body). For exampleemail/my_message
would map to templatesemail/my_message.subject.txt
,email/my_message.body.txt
,email/my_message.body.html
context (
dict
) – Template context dictionary passed to the rendererersender – Override the sender email - if not specific use the default set in the config as
mail.default_sender
immediate – Set True to send to the email immediately and do not wait the transaction to commit. This is very useful for debugging outgoing email issues in an interactive traceback inspector. If this is
None
then use settingmail.immediate
that defaults toFalse
.tm (
Optional
[TransactionManager
]) – Give transaction manager that is used instead ofrequest.tm
for the commit hook.
- Return type
- Returns
tuple(subject, text_body, html_body)