Skip to content

Instantly share code, notes, and snippets.

@medmunds
Created January 15, 2015 22:13
Show Gist options
  • Save medmunds/45ab7efc79e2d71ad514 to your computer and use it in GitHub Desktop.
Save medmunds/45ab7efc79e2d71ad514 to your computer and use it in GitHub Desktop.
queue_message for use with django-mailer
# queue_message(message, priority="medium")
# Queues an EmailMessage object, in apps using django-mailer.
# (https://github.com/pinax/django-mailer)
# Respects django-mailer's DontSendEntry blacklist.
# If django-mailer is not loaded, just sends the message immediately.
from django.conf import settings
if "mailer" in settings.INSTALLED_APPS:
# Sigh. Django-mailer doesn't expose its queuing API in a useful way, so we replicate parts of it here.
# This is adapted from mailer.send_html_mail
from mailer import PRIORITY_MAPPING
from mailer.models import make_message
def queue_message(message, priority="medium"):
# need to force_unicode in case fields used lazy version of ugettext
message.subject = force_unicode(message.subject)
message.body = force_unicode(message.body)
queued_msg = make_message(
subject=message.subject,
body=message.body,
from_email=message.from_email,
to=message.to,
# cc=message.cc, # BUG: django-mailer doesn't check cc field for DontSendEntry
bcc=message.bcc,
priority=PRIORITY_MAPPING[priority])
# copy back any DontSendEntry recipient filtering from make_message
message.to = queued_msg.email.to
message.cc = queued_msg.email.cc
message.bcc = queued_msg.email.bcc
queued_msg.email = message
queued_msg.save()
return 1
else:
# No Django-mailer, so just send immediately
def queue_message(message, priority="medium"):
return message.send()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment