I know there are 20 questions similar to mine but I’ve tried for over a day now to get email to work with Django.
I’m getting this error: [Errno 111] Connection refused when I attempt to send an email
This is where I create the email and attempt to send it in my view:
try:
msg = EmailMessage(subject, message, from_email, [receiver])
msg.content_subtype = "html"
msg.send()
My settings file is as follows:
EMAIL_HOST = "localhost" DEFAULT_FROM_EMAIL = "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f52465a525e56537f58525e5653115c5052">[email protected]</a>" EMAIL_PORT = 25 EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
I’ve tried doing test sending using python -m smtpd -n -c DebuggingServer localhost:1025 and had success, but when it comes down to doing it for real, no success.
When I try doing a send_mail from the shell I get this traceback:
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'Test', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81ecf8e4ece0e8edc1e6ece0e8edafe2eeec">[email protected]</a>', ['<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="234e5a464e424a4f63444e424a4f0d404c4e">[email protected]</a>'])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
connection=connection).send()
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/message.py", line 251, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 79, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.6/dist-packages/django/core/mail/backends/smtp.py", line 42, in open
local_hostname=DNS_NAME.get_fqdn())
File "/usr/lib/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.6/socket.py", line 561, in create_connection
raise error, msg
error: [Errno 111] Connection refused
I just don’t seem to be getting anywhere with this. Any help or advice would be much appreciated. Thanks
Also, if there is something else you’d like to see, just comment about it.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Are you trying to use a gmail account? Maybe try this then:
EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f06100a0d520a0c1a0d111e121a3f18121e1613511c1012">[email protected]</a>' EMAIL_HOST_PASSWORD = 'your-password' EMAIL_PORT = 587 EMAIL_USE_TLS = True
Then try test (django < 1.4) by
python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test email', 'hello world', to=['<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f5b4a5c5b6f4a424e4643014c4042">[email protected]</a>'])
And if you use django 1.4 use this:
python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('test email', 'hello world', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d3422383f0d28202c2421632e2220">[email protected]</a>', ['<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b1f0e181f2b0e060a020745080406">[email protected]</a>'])
If you’re not using a gmail account and still getting problems then just try add the EMAIL_HOST_USER and EMAIL_HOST_PASSWORD to what you have.
If you still have issues maybe your network is blocking you. Firewalls on your OS or router.
Thanks to knite for the updated syntax. Throw him a +1 and thanks to pranavk for letting me know about the syntax change in django 1.4
Method 2
First Create an Application specific password
- Visit your Google Account security page. And Click 2-step verification:

- Click
App passwordsat Google Account security page:

Then add the appropriate values to settings.py:
EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="265f4953540b5355435448474b4366414b474f4a0845494b">[email protected]</a>' EMAIL_HOST_PASSWORD = 'Application spectific password(for eg: smbumqjiurmqrywn)' EMAIL_PORT = 587 EMAIL_USE_TLS = True
You can use the shell to test it:
python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('Test', 'This is a test', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="047d6b7176446169656d682a676b69">[email protected]</a>', ['<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="374358525a565e5b77525a565e5b1954585a">[email protected]</a>'],
fail_silently=False)
Method 3
@mongoose_za has a great answer, but the syntax is a bit different in Django 1.4+.
Instead of:
send_mail('test email', 'hello world', to=['<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfcbdacccbffdad2ded6d391dcd0d2">[email protected]</a>'])
use
send_mail('test email', 'hello world', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d4452484f7d58505c5451135e5250">[email protected]</a>', ['<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="067263757246636b676f6a2865696b">[email protected]</a>'])
The first four arguments are required: subject, message, from_email, and recipient_list.
Method 4
- Enable pop3 in gmail settings.
- create application specific password for this django application. (http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833)
Method 5
I would avoid using GMail. It will work for a few emails, but after that, you may find that all your emails are being rejected or spam-canned. I used Amazon’s “SES” service with Django-SES to solve this.
Method 6
In settings.py, Use smtp as backend and not console
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Method 7
put the following minimal settings in the settings.py or local_settings.py file on your server.
EMAIL_HOST = 'localhost' EMAIL_PORT = 587 EMAIL_USE_TLS = True
instead of using smtp.gmail.com which imposes lot many limitations, you can have your own mail server.
you can do it by installing your own mailserver:
sudo apt-get install sendmail
Method 8
First setup the email backend variables in settings.py file.
EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d1402181f40181e081f030c00082d0a000c0401430e0200">[email protected]</a>' EMAIL_HOST_PASSWORD = 'your-password' EMAIL_PORT = 587 EMAIL_USE_TLS = True
Then, create an email object to send the mails.
from django.core import mail connection = mail.get_connection() # Manually open the connection connection.open() # Construct an email message that uses the connection email = mail.EmailMessage( 'Hello', 'Body goes here', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5335213c3e13362b323e233f367d303c3e">[email protected]</a>', ['<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b6f742a5b7e637a766b777e35787476">[email protected]</a>'], connection=connection, ) email.send() # Send the email connection.close()
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

