'Django Sending Email : SMTPServerDisconnected: Connection unexpectedly closed
hello i want to sending email activation use django registration redux.
this is my setting.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
ACCOUNT_ACTIVATION_DAYS = 3
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'blahpassword'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
LOGIN_REDIRECT_URL = '/'
when i try with
python manage.py shell
from django.core.mail import send_mail
send_mail('Test', 'This is a test', '[email protected]', ['[email protected]'])
i am getting error like this:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/message.py", line 286, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 92, in send_messages
new_conn_created = self.open()
File "/home/apsijogja/djangogirls/myvenv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 50, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 310, in connect
(code, msg) = self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 361, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed
can you help me solve this problem?
Solution 1:[1]
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
Solution 2:[2]
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
Solution 3:[3]
Please look into this Link: https://code.djangoproject.com/ticket/9575 and try sending via shell, it should work
Solution 4:[4]
In my case using smtplib.SMTP_SSL() solve this problem. You can try this.
Solution 5:[5]
I'm using AWS SES and was having the same problem.
Using port 587 worked for me. Settings were pretty much identical to @User0511
Solution 6:[6]
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
Solution 7:[7]
Normally this is raised because wrong SENDGRID credentials here you have to use
EMAIL_HOST_USER = 'apikey'
Solution 8:[8]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
