2009-11-03 20:53:26 +08:00
|
|
|
"""SMTP email backend class."""
|
|
|
|
import smtplib
|
2013-01-04 05:12:11 +08:00
|
|
|
import ssl
|
2009-11-03 20:53:26 +08:00
|
|
|
import threading
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.mail.backends.base import BaseEmailBackend
|
|
|
|
from django.core.mail.utils import DNS_NAME
|
2011-01-15 13:55:24 +08:00
|
|
|
from django.core.mail.message import sanitize_address
|
2012-11-14 17:39:58 +08:00
|
|
|
from django.utils.encoding import force_bytes
|
2011-01-15 13:55:24 +08:00
|
|
|
|
2009-11-03 20:53:26 +08:00
|
|
|
|
|
|
|
class EmailBackend(BaseEmailBackend):
|
|
|
|
"""
|
|
|
|
A wrapper that manages the SMTP network connection.
|
|
|
|
"""
|
|
|
|
def __init__(self, host=None, port=None, username=None, password=None,
|
2013-10-15 23:18:06 +08:00
|
|
|
use_tls=None, fail_silently=False, use_ssl=None, timeout=None,
|
|
|
|
**kwargs):
|
2009-11-03 20:53:26 +08:00
|
|
|
super(EmailBackend, self).__init__(fail_silently=fail_silently)
|
|
|
|
self.host = host or settings.EMAIL_HOST
|
|
|
|
self.port = port or settings.EMAIL_PORT
|
2013-07-12 02:58:06 +08:00
|
|
|
self.username = settings.EMAIL_HOST_USER if username is None else username
|
|
|
|
self.password = settings.EMAIL_HOST_PASSWORD if password is None else password
|
|
|
|
self.use_tls = settings.EMAIL_USE_TLS if use_tls is None else use_tls
|
|
|
|
self.use_ssl = settings.EMAIL_USE_SSL if use_ssl is None else use_ssl
|
2013-10-15 23:18:06 +08:00
|
|
|
self.timeout = timeout
|
2013-07-12 02:58:06 +08:00
|
|
|
if self.use_ssl and self.use_tls:
|
|
|
|
raise ValueError(
|
|
|
|
"EMAIL_USE_TLS/EMAIL_USE_SSL are mutually exclusive, so only set "
|
|
|
|
"one of those settings to True.")
|
2009-11-03 20:53:26 +08:00
|
|
|
self.connection = None
|
|
|
|
self._lock = threading.RLock()
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
"""
|
|
|
|
Ensures we have a connection to the email server. Returns whether or
|
|
|
|
not a new connection was required (True or False).
|
|
|
|
"""
|
|
|
|
if self.connection:
|
|
|
|
# Nothing to do if the connection is already open.
|
|
|
|
return False
|
2013-10-15 23:18:06 +08:00
|
|
|
|
|
|
|
connection_class = smtplib.SMTP_SSL if self.use_ssl else smtplib.SMTP
|
|
|
|
# If local_hostname is not specified, socket.getfqdn() gets used.
|
|
|
|
# For performance, we use the cached FQDN for local_hostname.
|
|
|
|
connection_params = {'local_hostname': DNS_NAME.get_fqdn()}
|
|
|
|
if self.timeout is not None:
|
|
|
|
connection_params['timeout'] = self.timeout
|
2009-11-03 20:53:26 +08:00
|
|
|
try:
|
2013-10-15 23:18:06 +08:00
|
|
|
self.connection = connection_class(self.host, self.port, **connection_params)
|
|
|
|
|
|
|
|
# TLS/SSL are mutually exclusive, so only attempt TLS over
|
|
|
|
# non-secure connections.
|
|
|
|
if not self.use_ssl and self.use_tls:
|
|
|
|
self.connection.ehlo()
|
|
|
|
self.connection.starttls()
|
|
|
|
self.connection.ehlo()
|
2013-10-25 17:19:41 +08:00
|
|
|
if self.username and self.password:
|
|
|
|
self.connection.login(self.username, self.password)
|
2013-09-30 23:55:14 +08:00
|
|
|
except smtplib.SMTPException:
|
2009-11-03 20:53:26 +08:00
|
|
|
if not self.fail_silently:
|
|
|
|
raise
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""Closes the connection to the email server."""
|
2013-01-04 03:41:45 +08:00
|
|
|
if self.connection is None:
|
|
|
|
return
|
2009-11-03 20:53:26 +08:00
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self.connection.quit()
|
2013-01-04 05:12:11 +08:00
|
|
|
except (ssl.SSLError, smtplib.SMTPServerDisconnected):
|
2009-11-03 20:53:26 +08:00
|
|
|
# This happens when calling quit() on a TLS connection
|
2013-01-04 01:49:00 +08:00
|
|
|
# sometimes, or when the connection was already disconnected
|
|
|
|
# by the server.
|
2009-11-03 20:53:26 +08:00
|
|
|
self.connection.close()
|
2013-09-30 23:55:14 +08:00
|
|
|
except smtplib.SMTPException:
|
2009-11-03 20:53:26 +08:00
|
|
|
if self.fail_silently:
|
|
|
|
return
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
self.connection = None
|
|
|
|
|
|
|
|
def send_messages(self, email_messages):
|
|
|
|
"""
|
|
|
|
Sends one or more EmailMessage objects and returns the number of email
|
|
|
|
messages sent.
|
|
|
|
"""
|
|
|
|
if not email_messages:
|
|
|
|
return
|
2012-06-23 23:11:15 +08:00
|
|
|
with self._lock:
|
2009-11-03 20:53:26 +08:00
|
|
|
new_conn_created = self.open()
|
|
|
|
if not self.connection:
|
|
|
|
# We failed silently on open().
|
|
|
|
# Trying to send would be pointless.
|
|
|
|
return
|
|
|
|
num_sent = 0
|
|
|
|
for message in email_messages:
|
|
|
|
sent = self._send(message)
|
|
|
|
if sent:
|
|
|
|
num_sent += 1
|
|
|
|
if new_conn_created:
|
|
|
|
self.close()
|
|
|
|
return num_sent
|
|
|
|
|
|
|
|
def _send(self, email_message):
|
|
|
|
"""A helper method that does the actual sending."""
|
|
|
|
if not email_message.recipients():
|
|
|
|
return False
|
2011-01-15 13:55:24 +08:00
|
|
|
from_email = sanitize_address(email_message.from_email, email_message.encoding)
|
|
|
|
recipients = [sanitize_address(addr, email_message.encoding)
|
|
|
|
for addr in email_message.recipients()]
|
2012-11-14 17:39:58 +08:00
|
|
|
message = email_message.message()
|
|
|
|
charset = message.get_charset().get_output_charset() if message.get_charset() else 'utf-8'
|
2009-11-03 20:53:26 +08:00
|
|
|
try:
|
2010-12-21 23:26:49 +08:00
|
|
|
self.connection.sendmail(from_email, recipients,
|
2012-11-14 17:39:58 +08:00
|
|
|
force_bytes(message.as_string(), charset))
|
2013-09-30 23:55:14 +08:00
|
|
|
except smtplib.SMTPException:
|
2009-11-03 20:53:26 +08:00
|
|
|
if not self.fail_silently:
|
|
|
|
raise
|
|
|
|
return False
|
|
|
|
return True
|