0

I saw this question being asked at a number of places but no solutions have worked for me so far, or may be I am unable to understand what to do exactly (newbie traits). Please help if you can.

Email Settings/ settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'xyz@gmail.com'
EMAIL_HOST_PASSWORD = 'pwd'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587

Error I am receiving while sending test email from the Django Python Shell:

First I ran the send mail command - Gave me Error1: django.core.exceptions.ImproperlyConfigured:

>>> from django.core.mail import send_mail
>>> send_mail('Test', 'Test', 'xyz@gmail.com', ['abc@hotmail.com'], fail_silently=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-  packages/Django-1.8.2-py2.7.egg/django/core/mail/__init__.py", line 56, in send_mail
fail_silently=fail_silently)
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/__init__.py", line 37, in get_connection
klass = import_string(backend or settings.EMAIL_BACKEND)
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_BACKEND, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

To Overcome Error1- I ran the below commands

>>> from django.conf import settings
>>> settings.configure()

Secondly I ran the send mail again - Gave Error2: Emails not going out! socket.error: [Errno 111] Connection refused

>>> from django.core.mail import send_mail
>>> send_mail('Test', 'Test', 'xyz@gmail.com', ['abc@hotmail.com'], fail_silently=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/home/django_admin1/venv-tile-prod/lib/python2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/local/lib/python2.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.7/smtplib.py", line 311, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python2.7/smtplib.py", line 286, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/local/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 111] Connection refused
Utkarsh Sinha
  • 941
  • 2
  • 11
  • 30

1 Answers1

0

All this while I was attempting to send the email from the normal python prompt which is invoked as:

(venv)[user@server django_project]$  python

The emails never went. Actually they have to be run from

(venv)[user@server django_project]$  python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'This is test email', 'xyz@gmail.com', ['recipientemail@hotmail.com'], fail_silently=False)

That resolved the issue for me. The email went out perfectly. So there was no problem with my email setup, there was a problem in the way i was sending it.

Utkarsh Sinha
  • 941
  • 2
  • 11
  • 30