smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Question:

i am trying to execute the following code on terminal with django.core.mail send_mail

send_mail('some title','some text','[email protected]',['[email protected]'])

but after execution the console is showing this error

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/__init__.py", line 61, in send_mail
    return mail.send()
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "/home/adjtlikp/virtualenv/globalit-web/3.7/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 62, in open
    self.connection = self.connection_class(self.host, self.port, **connection_params)
  File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 338, in connect
    (code, msg) = self.getreply()
  File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 394, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

my settings.py

EMAIL_USE_TLS = True  
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'   
EMAIL_HOST = 'smtp.gmail.com'  
EMAIL_HOST_PASSWORD = 'mypass'  
EMAIL_HOST_USER = '[email protected]'   
EMAIL_PORT = 465 
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

i tryed to change the EMAIL_PORT and the EMAIL_HOST but nothing changed the intersting is that in localhost the code work perfect but when i am using the host the errors came to me

Asked By: Mher

||

Answers:

The error showed on because, I entered the wrong port for EMAIL_USE_TLS in case of TLS the port should be 25 or 587 and in case of SSL the port should be 465.

Answered By: Mher

I had the same case. My settings were:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = config('PASS')

I also had a verified email on SendGrid. The reason I was getting this error was because I was not using the email I had verified on SendGrid. If you’re using another email address to send an email from your website you might also get the following error:

(550, b’The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements’)

So in short, use only one existing email which you have verified on SendGrid then if the code works, check the inbox/spam of the email you used.

Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.