I am trying to send an email using Django send_email, smtp and gmail. However, the code returns SMTPConnectError (-1, ' ')
My settings.py file:
ALLOWED_HOSTS = []
DEFAULT_FROM_EMAIL = 'mygmailid'
SERVER_EMAIL = 'mygmailid'
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'mygmailid'
EMAIL_HOST_PASSWORD = 'myPassword'
EMAIL_PORT = 587
My views.py file:
from django.http import HttpResponse
from django.core.mail import send_mail
from smtplib import SMTPConnectError
#rest of the code in between ....
try:
send_mail('testmail', 'test content', settings.EMAIL_HOST_USER, ['arkagh...@gmail.com', fail_silently=False])
except SMTPConnectError as e:
return HttpResponse(e)
The traceback:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python34\lib\site-packages\django-1.8.2- py3.4.egg\django\core\mail\__
init__.py", line 62, in send_mail
return mail.send()
File "C:\Python34\lib\site-packages\django-1.8.2- py3.4.egg\django\core\mail\me
ssage.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "C:\Python34\lib\site-packages\django-1.8.2-py3.4.egg\django\core\mail\ba
ckends\smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "C:\Python34\lib\site-packages\django-1.8.2-py3.4.egg\django\core\mail\ba
ckends\smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params
)
File "C:\Python34\lib\smtplib.py", line 244, in __init__
raise SMTPConnectError(code, msg)
smtplib.SMTPConnectError: (-1, '')
When I change the EMAIL_BACKEND to:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
it works fine. Also, it doesn't show the error when I use dummy backend.
I have tried using SSL instead of TLS, unlocked captcha, allowed less secure apps in
gmail, turned off firewall and also tried other stackoverflow posts
related to sending email via django but nothing worked. I don't know
what the error is. Please help me. Thanks in advance.