Skip to main content

A reusable Django app for queuing the sending of email

Project description

django-mailer is asynchronous so in addition to putting mail on the queue you need to periodically tell it to clear the queue and actually send the mail.

The latter is done via a command extension.

Putting Mail On The Queue

Because django-mailer currently uses the same function signature as Django’s core mail support you can do the following in your code:

# 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)

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:

* * * * * (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.

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-0.1.0.tar.gz (10.3 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for django-mailer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 803cc1b41ba373722b59992307bdd968cc534961fdaae8442c705235eb6e6353
MD5 b8bea74a7a0e6b0ebab68089e68df62c
BLAKE2b-256 d3fb2258945afae50359c323aea826a74ec72f4c6b143635c5dd1fff5e0e17b0

See more details on using hashes here.

Supported by

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