Reputation: 155
I am trying to send an email using django.core.mail.send_email()
and constantly getting the ConnectionRefusedError: [Errno 111] Connection refused
with the following traceback:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/EmilKadermetov/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/core/mail/__init__.py", line 61, in send_mail
return mail.send()
File "/home/EmilKadermetov/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/core/mail/message.py", line 284, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/EmilKadermetov/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "/home/EmilKadermetov/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 62, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python3.8/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.8/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.8/smtplib.py", line 308, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/lib/python3.8/socket.py", line 807, in create_connection
raise err
File "/usr/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
settings.py
:EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOSTS = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'my_passwod'
"Less security app access" in my google account is turned on.
I can successfully connect to the smtp.gmail.com
via telnet from the same machine.
Using another smtp host changes nothing.
Django3.1.6 | Python 3.8.5 | Linux Mint 20.1
Upvotes: 1
Views: 3203
Reputation: 311
it helped me to add in settingd.py:
RECIPIENTS_EMAIL = ['[email protected]'] # замените на свою почту
DEFAULT_FROM_EMAIL = '[email protected]' # замените на свою почту
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Upvotes: 1
Reputation: 155
It was just a typo in the settings.py
file's line which specify a SMTP host: EMAIL_HOSTS = 'smtp.gmail.com'
instead of EMAIL_HOST = 'smtp.gmail.com'
. So Django used the default EMAIL_HOST
parameter - localhost
and was failing to connect. Thanks people in comments that tried to help me.
Upvotes: 0