Skip to content

Instantly share code, notes, and snippets.

@jbalogh
Created July 17, 2010 00:06
Show Gist options
  • Select an option

  • Save jbalogh/479089 to your computer and use it in GitHub Desktop.

Select an option

Save jbalogh/479089 to your computer and use it in GitHub Desktop.
"""SMTP email backend class."""
from django.core.mail.backends import smtp
class EmailBackend(smtp.EmailBackend):
"""
A wrapper that manages the SMTP network connection.
"""
def send_messages(self, email_messages):
"""
Sends one or more EmailMessage objects and returns a tuple of
(successful messages, failed messages).
"""
if not email_messages:
return
self._lock.acquire()
success, failed = [], []
try:
new_conn_created = self.open()
if not self.connection:
# We failed silently on open().
# Trying to send would be pointless.
return [], email_messages
for message in email_messages:
sent = self._send(message)
if sent:
success.append(message)
else:
failed.append(message)
if new_conn_created:
self.close()
finally:
self._lock.release()
return success, failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment