I am trying to send an email using Django send_email, smtp and gmail. However, the code returns SMTPConnectError (-1, b ")
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
....
try:
send_mail('testmail', 'test content', settings.EMAIL_HOST_USER, ['arkaghosh024@gmail.com', fail_silently=False])
except SMTPConnectError as e:
return HttpResponse(e)
Track:
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, b")
When I change EMAIL_BACKEND to:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
It works great. In addition, it does not show an error when I use a dummy server.
I tried using SSL instead of TLS, allowed less secure applications in gmail, turned off the firewall, and also tried other stackoverflow entries related to sending email through django, but nothing worked. I do not know what kind of mistake this is. Please help me. Thanks in advance.