Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django email not working - smtplib.SMTPServerDisconnected: Connection unexpectedly closed

I'm using the standard Django/SendGrid setup for sending emails. Here's the relevant fields in my settings.py:

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'myusername'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
DEFAULT_FROM_EMAIL = '[email protected]'

I'm testing sending emails in my shell by executing:

send_mail('test','test','[email protected]',['[email protected]'])

however, it returns this error Traceback:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/mail/message.py", line 348, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/mail/backends/smtp.py", line 104, in send_messages
    new_conn_created = self.open()
  File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/mail/backends/smtp.py", line 71, in open
    self.connection.login(force_str(self.username), force_str(self.password))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py", line 720, in login
    initial_response_ok=initial_response_ok)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py", line 630, in auth
    (code, resp) = self.docmd("AUTH", mechanism + " " + response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py", line 420, in docmd
    return self.getreply()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py", line 393, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Any idea why I'm getting this error?

PS: This is my local development server

like image 817
Zorgan Avatar asked Mar 26 '18 11:03

Zorgan


1 Answers

Updated 2021

Sendgrid now requires using API Key instead of username/password:

Two-Factor Authentication is required as of Q4 2020, and all Twilio SendGrid API endpoints will reject new API requests and SMTP configurations made with a username and password via Basic Authentication.

So you'll need to create an API Key with at least Mail Send > Full Access permissions, then tweak your Django configs like this:

EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY

Sample code:

settings.py

SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True

utils.py

from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', '[email protected]', ['[email protected]'], fail_silently=False)
like image 95
Andre Pereira Avatar answered Sep 16 '22 12:09

Andre Pereira