Managers
EmailManager
pragmatic.managers.EmailManager
Sends HTML and/or plain-text emails rendered from Django templates, with optional background dispatch via RQ.
Usage
from pragmatic.managers import EmailManager
EmailManager.send_mail(
to=user, # User instance or email string, or list of either
template_prefix='emails/welcome', # loads welcome.txt and/or welcome.html
subject='Welcome to our service',
data={'activation_link': url}, # extra template context
request=request, # optional; adds site to context
attachments=[ # optional file attachments
{
'filename': 'invoice.pdf',
'content': pdf_bytes,
'content_type': 'application/pdf',
}
],
reply_to=support_user, # optional reply-to address
)
Template discovery
EmailManager.send_mail looks for:
{template_prefix}.txt— plain-text body (optional){template_prefix}.html— HTML alternative (optional)
At least one of the two should exist. The rendered template receives:
Variable |
Value |
|---|---|
|
The subject string passed to |
|
The request object (or |
|
Result of |
|
The Django settings module |
|
The |
(custom) |
Any keys from the |
Background sending
When MAILS_QUEUE is set in settings, emails are enqueued via
send_mail_in_background.delay(email) instead of being sent
synchronously. Requires django-rq to be installed and configured.
# settings.py
MAILS_QUEUE = 'default' # name of an RQ queue
When MAILS_QUEUE is None (the default), email.send() is called
immediately and its return value is returned by send_mail.
Helper methods
EmailManager.get_recipient(to)Returns
toif it is already a string, otherwiseto.email.EmailManager.get_recipients(to)Returns a list of email address strings, accepting a single object, a string, or a list of either.