[Django]-Django Sending Email : SMTPServerDisconnected: Connection unexpectedly closed

25👍

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

if you want use PORT 465,

your EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'

EMAIL_PORT=465

and you need to install django_smtp_ssl

otherwise you can keep,

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_PORT=465

3👍

For python3.0+

I found a workaround or maybe this is the solution , there is some issue in the django email package.

1)pip install sendgrid-django

2)Add these in the settings.py along with other email configurations

  EMAIL_BACKEND = 'sgbackend.SendGridBackend'
  SENDGRID_API_KEY = "YOUR SENDGRID API KEY"
  EMAIL_PORT = 465

3)Code sample to send mail(yours maybe different):

from django.core.mail import EmailMessage
send_email = EmailMessage(
    subject=subject,
    body=body,
    from_email=from_email,
    to=to_email,
    reply_to=reply_to,
    headers=headers,

)
send_email.send(fail_silently=fail_silently)

You have to certainly make changes to the code as i was just showing a example.

For debugging purpose only-sendgrid mail send using telnet

2👍

Please look into this Link: https://code.djangoproject.com/ticket/9575
and try sending via shell, it should work

👤nyk91

2👍

I was developing and testing on localhost with the Gmail SMTP server and ran into this error. I figured out this was because I was using the EMAIL_PORT = 465 on a EMAIL_USE_TLS = True configuration.

EMAIL_PORT = 465 is an SSL port configuration and is supposed to be used alongside the EMAIL_USE_SSL = True configuration and not on a EMAIL_USE_TLS = True configuration.

So anybody that will run into this issue in the future with same mistake as me should change that and everything should work fine. Below is what the full SMTP configuration looked like for me:

# Email server configuration
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' 
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_gmail_account@gmail.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxxxxxxxxxx'
EMAIL_PORT = 465
EMAIL_USE_SSL = True

Also note that using TLS with EMAIL_PORT = 587 and EMAIL_USE_TLS = True didn’t work for me on Gmail, so I had to switch to using SSL and it worked.

1👍

In my case using smtplib.SMTP_SSL() solve this problem. You can try this.

1👍

Like myself, if your deployed django app is on heroku and using https meaning you need port 465, you should not forget to add these same values below here to your heroku Config Vars. It threw ‘connection unexpectedly closed’ error untill i had to add this.

# settings.py

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')                   
                     # replace here with the long SendGrid API secret code 
                     # xyxyxyxyzzzyz on heroku's Config Vars
EMAIL_PORT =  465 
EMAIL_USE_SSL = True

Without that, debugging on localhost or development sever will work (with port 587 under http) but not at deployment under port 465 with https

1👍

I’m using AWS SES and was having the same problem.

Using port 587 worked for me. Settings were pretty much identical to @User0511

1👍

In my case the problem was that I used my api for EMAIL_HOST_PASSWORD.
Then I found my SMPT password by going to https://app.mailgun.com/app/sending/domains/
Clicked on my domain name
Then Selected on SMPT

0👍

Normally this is raised because wrong SENDGRID credentials
here you have to use

EMAIL_HOST_USER = ‘apikey’

Leave a comment