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