tcostam / django-templated-email

Django module to easily send templated emails using django templates, or using a transactional mail provider (mailchimp, silverpop, etc.)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Django-Templated-Email

GitterBadge_ PypiversionBadge_ PythonVersionsBadge_ LicenseBadge_

Info

A Django oriented templated email sending class

Author

Bradley Whittington (http://github.com/bradwhittington, http://twitter.com/darb)

Tests

TravisBadge_ CoverageBadge_

Overview

django-templated-email is oriented towards sending templated emails. The library supports template inheritance, adding cc'd and bcc'd recipients, configurable template naming and location.

The send_templated_email method can be thought of as the render_to_response shortcut for email.

Make sure you are reading the correct documentation:

develop branch: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst

stable pypi/master: https://github.com/vintasoftware/django-templated-email/blob/master/README.rst

Getting going - installation

Installing:

pip install django-templated-email

You can add the following to your settings.py (but it works out the box):

Sending templated emails

Example usage using vanilla_django TemplateBackend backend

Python to send mail:

If you would like finer control on sending the email, you can use get_templated_email, which will return a django EmailMessage object, prepared using the vanilla_django backend:

You can also cc and bcc recipients using cc=['example@example.com'].

Your template

The templated_email/ directory needs to be the templates directory.

The backend will look in my_app/templates/templated_email/welcome.email :

If you want to include an HTML part to your emails, simply use the 'html' block :

The plain part can also be calculated from the HTML using html2text. If you don't specify the plain block and html2text package is installed, the plain part will be calculated from the HTML part. You can disable this behaviour in settings.py :

You can also specify a custom function that converts from HTML to the plain part :

You can globally override the template dir, and file extension using the following variables in settings.py :

You can also set a value for template_prefix and template_suffix for every time you call send_templated_mail, if you wish to store a set of templates in a different directory. Remember to include a trailing slash.

Using with Django Anymail

Anymail integrates several transactional email service providers (ESPs) into Django, with a consistent API that lets you use ESP-added features without locking your code to a particular ESP. It supports Mailgun, Postmark, SendGrid, SparkPost and more.

You can use it with django-templated-email, just follow their instructions in their quick start to configure it.

Optionally you can use their custom EmailMessage class with django-templated-email by using the following settings:

Inline images

You can add inline images to your email using the InlineImage class.

First get the image content from a file or a ImageField:

Then create an instance of InlineImage:

Now pass the object on the context to the template when you send the email.

Finally in your template add the image on the html template block:

Note: All InlineImage objects you add to the context will be attached to the e-mail, even if they are not used in the template.

And, finally add the link to your template.

Notes:
  • A copy of the rendered e-mail will be stored on the database. This can grow if you send too many e-mails. You are responsible for managing it.
  • If you use InlineImage all images will be uploaded to your media storage, keep that in mind too.

Class Based Views

It's pretty common for emails to be sent after a form is submitted. We include a mixin to be used with any view that inherit from Django's FormMixin.

In your view add the mixin and the usual Django's attributes:

By default the template will have the form_data if the form is valid or from_errors if the form is not valid in it's context.

You can view an example here

Now you can use the following attributes/methods to customize it's behavior:

Attributes:

templated_email_template_name (mandatory if you don't implement templated_email_get_template_names()):

String naming the template you want to use for the email. ie: templated_email_template_name = 'welcome'.

templated_email_send_on_success (default: True):

This attribute tells django-templated-email to send an email if the form is valid.

templated_email_send_on_failure (default: False):

This attribute tells django-templated-email to send an email if the form is invalid.

templated_email_from_email (default: settings.TEMPLATED_EMAIL_FROM_EMAIL):

String containing the email to send the email from.

Methods:

templated_email_get_template_names(self, valid) (mandatory if you don't set templated_email_template_name):

If the method returns a string it will use it as the template to render the email. If it returns a list it will send the email only with the first existing template.

templated_email_get_recipients(self, form) (mandatory):

Return the recipient list to whom the email will be sent to. ie:

templated_email_get_context_data(kwargs)** (optional):

Use this method to add extra data to the context used for rendering the template. You should get the parent class's context from calling super. ie:

templated_email_get_send_email_kwargs(self, valid, form) (optional):

Add or change the kwargs that will be used to send the e-mail. You should call super to get the default kwargs. ie:

templated_email_send_templated_mail(*args,kwargs)** (optional):

This method calls django-templated-email's send_templated_mail method. You could change this method to use a celery's task for example or to handle errors.

Future Plans

See https://github.com/vintasoftware/django-templated-email/issues?state=open

Using django_templated_email in 3rd party applications

If you would like to use django_templated_email to handle mail in a reusable application, you should note that:

  • Your calls to send_templated_mail should set a value for template_dir, so you can keep copies of your app-specific templates local to your app (although the loader will find your email templates if you store them in <your app>/templates/templated_email, if TEMPLATED_EMAIL_TEMPLATE_DIR has not been overidden)
  • If you do (and you should) set a value for template_dir, remember to include a trailing slash, i.e. 'my_app_email/'
  • The deployed app may use a different backend which doesn't use the django templating backend, and as such make a note in your README warning developers that if they are using django_templated_email already, with a different backend, they will need to ensure their email provider can send all your templates (ideally enumerate those somewhere convenient)

Notes on specific backends

Using vanilla_django

This is the default backend, and as such requires no special configuration, and will work out of the box. By default it assumes the following settings (should you wish to override them):

For legacy purposes you can specify email subjects in your settings file (but, the preferred method is to use a {% block subject %} in your template):

Additionally you can call send_templated_mail and optionally override the following parameters:

template_prefix='your_template_dir/'  # Override where the method looks for email templates (alternatively, use template_dir)
template_suffix='email'               # Override the file extension of the email templates (alternatively, use file_extension)
cc=['fubar@example.com']              # Set a CC on the mail
bcc=['fubar@example.com']             # Set a BCC on the mail
template_dir='your_template_dir/'     # Override where the method looks for email templates
connection=your_connection            # Takes a django mail backend connection, created using **django.core.mail.get_connection**
auth_user='username'                  # Override the user that the django mail backend uses, per **django.core.mail.send_mail**
auth_password='password'              # Override the password that the django mail backend uses, per **django.core.mail.send_mail**

Releasing a new version of this package:

Update CHANGELOG file.

Execute the following commands:

bumpversion [major,minor,patch]
python setup.py publish
git push origin master --tags

Commercial Support

This library, as others, is used in projects of Vinta clients. We are always looking for exciting work, so if you need any commercial support, feel free to get in touch: contact@vinta.com.br

About

Django module to easily send templated emails using django templates, or using a transactional mail provider (mailchimp, silverpop, etc.)

License:MIT License


Languages

Language:Python 99.9%Language:HTML 0.1%