EMAIL_BACKEND doesn't works with Amazon SES!?

695 views
Skip to first unread message

Neto

unread,
Mar 28, 2016, 8:56:12 PM3/28/16
to Django users
I'm trying to send emails with Amazon SES, but when I use default EMAIL_BACKEND raise error:

Config:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # this is default
EMAIL_HOST
= 'email-smtp...amazonaws.com'
EMAIL_PORT
= 465
EMAIL_HOST_USER
= '...'
EMAIL_HOST_PASSWORD
= '...'
EMAIL_USE_TLS
= True

Error:

raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib
.SMTPServerDisconnected: Connection unexpectedly closed

If I use:
 
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'

from https://github.com/bancek/django-smtp-ssl, works right.

Code of this backend:

import smtplib
 
from django.core.mail.utils import DNS_NAME
from django.core.mail.backends.smtp import EmailBackend
 
class SSLEmailBackend(EmailBackend):
  def open(self):
   
if self.connection:
     
return False
   
try:
     
self.connection = smtplib.SMTP_SSL(self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
     
if self.username and self.password:
       
self.connection.login(self.username, self.password)
     
return True
   
except:
     
if not self.fail_silently:
       
raise

What the problem with default EMAIL_BACKEND? Why I can't use him?

Raffaele Salmaso

unread,
Mar 29, 2016, 2:03:54 AM3/29/16
to django...@googlegroups.com
On Tue, Mar 29, 2016 at 2:56 AM, Neto <paulosou...@gmail.com> wrote:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # this is default
EMAIL_HOST
= 'email-smtp...amazonaws.com'
EMAIL_PORT
= 465
EMAIL_HOST_USER
= '...'
EMAIL_HOST_PASSWORD
= '...'
EMAIL_USE_TLS
= True

My config
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = "email-smtp.$zone.amazonaws.com"
EMAIL_PORT = 587
EMAIL_HOST_USER = "$USER"
EMAIL_HOST_PASSWORD = "$PASSWORD"
EMAIL_USE_TLS = True

Different port.
I'm using python 3.4

--

Michal Petrucha

unread,
Mar 29, 2016, 3:35:18 AM3/29/16
to django...@googlegroups.com
On Mon, Mar 28, 2016 at 05:56:12PM -0700, Neto wrote:
> I'm trying to send emails with Amazon SES, but when I use default
> EMAIL_BACKEND raise error:
>
> Config:
>
> EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # this is
> default
> EMAIL_HOST = 'email-smtp...amazonaws.com'
> EMAIL_PORT = 465
> EMAIL_HOST_USER = '...'
> EMAIL_HOST_PASSWORD = '...'
> EMAIL_USE_TLS = True
>
> Error:
>
> raise SMTPServerDisconnected("Connection unexpectedly closed")
> smtplib.SMTPServerDisconnected: Connection unexpectedly closed

The problem is that there are two different ways of establishing a TLS
connection to an SMTP server. One is the so-called SMTPS, which is
commonly used on port 465, where the client opens a TLS tunnel right
away, and all SMTP communication happens through this tunnel.

The other option is STARTTLS, which is normally supported on ports 25
and 587. With this protocol, the client first opens a regular
plain-text SMTP session with the server, and they exchange the
STARTTLS SMTP command, after which they perform the TLS handshake, and
only then is everything transferred over a secure TLS tunnel.

Django supports both protocols, however, these days SMTPS is
considered obsolete by some people, and a lot of folks will tell you
that STARTTLS is the way to go.

You set EMAIL_USE_TLS to True in your config, this tells Django to use
the STARTTLS protocol, but you also set it to connect to a port on the
server where it expects the SMTPS protocol instead. If you want to use
SMTPS (which is what you need to do in order to connect to port 465
successfully), you need to set EMAIL_USE_TLS to False, and instead set
EMAIL_USE_SSL to True.

The other option is, as Raffaele suggested in another reply, to keep
using EMAIL_USE_TLS, and switch the port to 587.

Good luck,

Michal
signature.asc

Neto

unread,
Mar 29, 2016, 12:07:43 PM3/29/16
to Django users
Thanks guys, 587 worked

Fred Stluka

unread,
Mar 29, 2016, 6:47:13 PM3/29/16
to django...@googlegroups.com
Good explanation, Michal!  Thanks!

--Fred
Fred Stluka -- mailto:fr...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.

ling...@gmail.com

unread,
Nov 12, 2020, 10:27:26 AM11/12/20
to Django users
This exception is raised when the server unexpectedly disconnects, or when an attempt is made to use the python mail SMTP instance before connecting it to a server. Clients sending outgoing mail should connect on port 587 and use starttls. To use port 465, you need to call smtplib.SMTP_SSL(). Currently, it calls smtplib.SMTP() .. so,change your PORT from 465 into 587 it.  Also, you'll need to send the ehlo command before the starttls command, then again after the starttls command. 
Reply all
Reply to author
Forward
0 new messages