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
|
2011-01-15 13:55:24 +08:00
|
|
|
from django.core.mail.message import sanitize_address
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.mail.utils import DNS_NAME
|
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,
|
2014-05-18 15:56:55 +08:00
|
|
|
ssl_keyfile=None, ssl_certfile=None,
|
2013-10-15 23:18:06 +08:00
|
|
|
**kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(fail_silently=fail_silently)
|
2009-11-03 20:53:26 +08:00
|
|
|
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()
|
|
|
|
|
2016-08-29 20:37:03 +08:00
|
|
|
@property
|
|
|
|
def connection_class(self):
|
|
|
|
return smtplib.SMTP_SSL if self.use_ssl else smtplib.SMTP
|
|
|
|
|
2009-11-03 20:53:26 +08:00
|
|
|
def open(self):
|
|
|
|
"""
|
2016-09-21 01:34:01 +08:00
|
|
|
Ensure an open connection to the email server. Return whether or not a
|
|
|
|
new connection was required (True or False) or None if an exception
|
|
|
|
passed silently.
|
2009-11-03 20:53:26 +08:00
|
|
|
"""
|
|
|
|
if self.connection:
|
|
|
|
# Nothing to do if the connection is already open.
|
|
|
|
return False
|
2013-10-15 23:18:06 +08:00
|
|
|
|
|
|
|
# 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:
|
2016-08-29 20:37:03 +08:00
|
|
|
self.connection = self.connection_class(self.host, self.port, **connection_params)
|
2013-10-15 23:18:06 +08:00
|
|
|
|
|
|
|
# TLS/SSL are mutually exclusive, so only attempt TLS over
|
|
|
|
# non-secure connections.
|
|
|
|
if not self.use_ssl and self.use_tls:
|
2014-05-18 15:56:55 +08:00
|
|
|
self.connection.starttls(keyfile=self.ssl_keyfile, certfile=self.ssl_certfile)
|
2013-10-25 17:19:41 +08:00
|
|
|
if self.username and self.password:
|
2017-01-12 06:17:25 +08:00
|
|
|
self.connection.login(self.username, self.password)
|
2014-01-13 07:33:48 +08:00
|
|
|
return True
|
2019-01-28 23:01:35 +08:00
|
|
|
except OSError:
|
2009-11-03 20:53:26 +08:00
|
|
|
if not self.fail_silently:
|
|
|
|
raise
|
|
|
|
|
|
|
|
def close(self):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Close 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):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Send one or more EmailMessage objects and return the number of email
|
2009-11-03 20:53:26 +08:00
|
|
|
messages sent.
|
|
|
|
"""
|
|
|
|
if not email_messages:
|
2018-12-23 09:06:56 +08:00
|
|
|
return 0
|
2012-06-23 23:11:15 +08:00
|
|
|
with self._lock:
|
2009-11-03 20:53:26 +08:00
|
|
|
new_conn_created = self.open()
|
2016-09-21 01:34:01 +08:00
|
|
|
if not self.connection or new_conn_created is None:
|
2009-11-03 20:53:26 +08:00
|
|
|
# We failed silently on open().
|
|
|
|
# Trying to send would be pointless.
|
2018-12-23 09:06:56 +08:00
|
|
|
return 0
|
2009-11-03 20:53:26 +08:00
|
|
|
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
|
2016-04-02 17:41:47 +08:00
|
|
|
encoding = email_message.encoding or settings.DEFAULT_CHARSET
|
|
|
|
from_email = sanitize_address(email_message.from_email, encoding)
|
|
|
|
recipients = [sanitize_address(addr, 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:
|
2014-10-09 20:18:31 +08:00
|
|
|
self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
|
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
|