Skip to main content

A reusable Django app for queuing the sending of email

Project description

Usage

Using EMAIL_BACKEND

This is the preferred and easiest way to use django-mailer.

To automatically switch all your mail to use django-mailer, first set EMAIL_BACKEND:

EMAIL_BACKEND = "mailer.backend.DbBackend"

If you were previously using a non-default EMAIL_BACKEND, you need to configure the MAILER_EMAIL_BACKEND setting, so that django-mailer knows how to actually send the mail:

MAILER_EMAIL_BACKEND = "your.actual.EmailBackend"

Now, just use the normal Django mail functions for sending email. These functions will store mail on a queue in the database, which must be sent as below.

Explicitly putting mail on the queue

If you don’t want to send all email through django-mailer, you can send mail using mailer.send_mail, which has the same signature as Django’s send_mail function.

You can also do the following:

# favour django-mailer but fall back to django.core.mail
from django.conf import settings

if "mailer" in settings.INSTALLED_APPS:
    from mailer import send_mail
else:
    from django.core.mail import send_mail

and then just call send_mail like you normally would in Django:

send_mail(subject, message_body, settings.DEFAULT_FROM_EMAIL, recipients)

There is also a convenience function mailer.send_html_mail for creating HTML (this function is not in Django):

send_html_mail(subject, message_plaintext, message_html, settings.DEFAULT_FROM_EMAIL, recipients)

Additionally you can send all the admins as specified in the ADMIN setting by calling:

mail_admins(subject, message_body)

or all managers as defined in the MANAGERS setting by calling:

mail_managers(subject, message_body)

Clear queue with command extensions

With mailer in your INSTALLED_APPS, there will be two new manage.py commands you can run:

  • send_mail will clear the current message queue. If there are any failures, they will be marked deferred and will not be attempted again by send_mail.

  • retry_deferred will move any deferred mail back into the normal queue (so it will be attempted again on the next send_mail).

You may want to set these up via cron to run regularly:

*       * * * * (/path/to/your/python /path/to/your/manage.py send_mail >> ~/cron_mail.log 2>&1)
0,20,40 * * * * (/path/to/your/python /path/to/your/manage.py retry_deferred >> ~/cron_mail_deferred.log 2>&1)

For use in Pinax, for example, that might look like:

* * * * * (cd $PINAX; /usr/local/bin/python2.5 manage.py send_mail >> $PINAX/cron_mail.log 2>&1)
0,20,40 * * * * (cd $PINAX; /usr/local/bin/python2.5 manage.py retry_deferred >> $PINAX/cron_mail_deferred.log 2>&1)

This attempts to send mail every minute with a retry on failure every 20 minutes.

manage.py send_mail uses a lock file in case clearing the queue takes longer than the interval between calling manage.py send_mail.

Note that if your project lives inside a virtualenv, you also have to execute this command from the virtualenv. The same, naturally, applies also if you’re executing it with cron. The Pinax documentation explains that in more details.

Using EMAIL_BACKEND

To automatically switch all your mail to use django-mailer, instead of changing imports you can also use the EMAIL_BACKEND feature that was introduced in Django 1.2. In your settings file, you first have to set EMAIL_BACKEND:

EMAIL_BACKEND = "mailer.backend.DbBackend"

If you were previously using a non-default EMAIL_BACKEND, you need to configure the MAILER_EMAIL_BACKEND setting, so that django-mailer knows how to actually send the mail:

MAILER_EMAIL_BACKEND = "your.actual.EmailBackend"

Controlling the delivery process

If you wish to have a finer control over the delivery process, which defaults to deliver everything in the queue, you can use the following 3 variables (default values shown):

MAILER_EMAIL_MAX_BATCH = None  # integer or None
MAILER_EMAIL_MAX_DEFERRED = None  # integer or None
MAILER_EMAIL_THROTTLE = 0  # passed to time.sleep()

These control how many emails are sent successfully before stopping the current run MAILER_EMAIL_MAX_BATCH, after how many failed/deferred emails should it stop MAILER_EMAIL_MAX_DEFERRED and how much time to wait between each email MAILER_EMAIL_THROTTLE.

Unprocessed emails will be evaluated in the following delivery iterations.

Change log

1.0.0

  • Throttling of email sending

  • Django 1.8 support

  • Admin tweaks and improvements

  • Included migrations - for both South and Django 1.7 native migrations.

    Note:

    • If you use South, you will need at least South 1.0

    • You will need to use ‘–fake’ or ‘–fake-initial’ on existing installations.

  • Various other fixes, especially from Renato Alves <alves.rjc@gmail.com> - thank you!

0.1.0

  • First PyPI version

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-mailer-1.0.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

django_mailer-1.0-py2-none-any.whl (19.1 kB view details)

Uploaded Python 2

File details

Details for the file django-mailer-1.0.tar.gz.

File metadata

  • Download URL: django-mailer-1.0.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for django-mailer-1.0.tar.gz
Algorithm Hash digest
SHA256 efc9a7acc16993c67a93ad38c57b2ed898c5d5c5b682a9865e903a0b1b2db96a
MD5 9bc4a29434800ca6d7883d392d9d8fcc
BLAKE2b-256 facf33dc0261935659e3d1f4538412343501c0e85b705936b06fd3d159e7626f

See more details on using hashes here.

File details

Details for the file django_mailer-1.0-py2-none-any.whl.

File metadata

File hashes

Hashes for django_mailer-1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 1cdcf83f2f367e81d3dde622e4d6780672f4cbf7174c624db3d657b957c05870
MD5 b3181d3811d280ff3a84505ef6c4ae46
BLAKE2b-256 459a63879839c0519c09d79215b99e39d3f56f0690a05b7ee7ff8825c4389285

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page