Permalink
Browse files

Added a Django 1.2 'EmailBackend' implementation.

  • Loading branch information...
1 parent 1119cd3 commit fada4e1b470af16e5c86f3ef59ce68af5f55e67c @spookylukey committed May 24, 2010
Showing with 31 additions and 1 deletion.
  1. +15 −0 docs/usage.txt
  2. +13 −0 mailer/backend.py
  3. +3 −1 mailer/engine.py
View
@@ -68,3 +68,18 @@ executing it with cron. The `Pinax documentation`_ explains that in more
details.
.. _pinax documentation: http://pinaxproject.com/docs/dev/deployment.html#sending-mail-and-notices
+
+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 setting_EMAIL_BACKEND, so that django-mailer knows how to actually send
+the mail::
+
+ MAILER_EMAIL_BACKEND = "your.actual.EmailBackend"
View
@@ -0,0 +1,13 @@
+from django.core.mail.backends.base import BaseEmailBackend
+from mailer.models import Message
+
+class DbBackend(BaseEmailBackend):
+
+ def send_messages(self, email_messages):
+ num_sent = 0
+ for email in email_messages:
+ msg = Message()
+ msg.email = email
+ msg.save()
+ num_sent += 1
+ return num_sent
View
@@ -17,6 +17,8 @@
# default behavior is to never wait for the lock to be available.
LOCK_WAIT_TIMEOUT = getattr(settings, "MAILER_LOCK_WAIT_TIMEOUT", -1)
+# The actual backend to use for sending, defaulting to the Django default.
+EMAIL_BACKEND = getattr(settings, "MAILER_EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend")
def prioritize():
"""
@@ -65,7 +67,7 @@ def send_all():
for message in prioritize():
try:
if connection is None:
- connection = get_connection()
+ connection = get_connection(backend=EMAIL_BACKEND)
logging.info("sending message '%s' to %s" % (message.subject.encode("utf-8"), u", ".join(message.to_addresses).encode("utf-8")))
email = message.email
email.connection = connection

0 comments on commit fada4e1

Please sign in to comment.