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
|
|
|
|
|
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,
|
2014-05-18 15:56:55 +08:00
|
|
|
ssl_keyfile=None, ssl_certfile=None,
|
2013-10-15 23:18:06 +08:00
|
|
|
**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
|
2014-09-13 10:46:22 +08:00
|
|
|
self.timeout = settings.EMAIL_TIMEOUT if timeout is None else timeout
|
2014-05-18 15:56:55 +08:00
|
|
|
self.ssl_keyfile = settings.EMAIL_SSL_KEYFILE if ssl_keyfile is None else ssl_keyfile
|
|
|
|
self.ssl_certfile = settings.EMAIL_SSL_CERTFILE if ssl_certfile is None else ssl_certfile
|
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
|
2014-05-18 15:56:55 +08:00
|
|
|
if self.use_ssl:
|
|
|
|
connection_params.update({
|
|
|
|
'keyfile': self.ssl_keyfile,
|
|
|
|
'certfile': self.ssl_certfile,
|
|
|
|
})
|
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()
|
2014-05-18 15:56:55 +08:00
|
|
|
self.connection.starttls(keyfile=self.ssl_keyfile, certfile=self.ssl_certfile)
|
2013-10-15 23:18:06 +08:00
|
|
|
self.connection.ehlo()
|
2013-10-25 17:19:41 +08:00
|
|
|
if self.username and self.password:
|
|
|
|
self.connection.login(self.username, self.password)
|
2014-01-13 07:33:48 +08:00
|
|
|
return True
|
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()
|
2009-11-03 20:53:26 +08:00
|
|
|
try:
|
2013-12-29 01:35:17 +08:00
|
|
|
self.connection.sendmail(from_email, recipients, message.as_bytes())
|
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
|