39

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 = "myemail@gmail.com"
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', 'myemail@gmail.com', ['myemail@gmail.com'])
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.

jamylak
  • 128,818
  • 30
  • 231
  • 230
Luke
  • 2,053
  • 1
  • 18
  • 25

8 Answers8

77

Are you trying to use a gmail account? Maybe try this then:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username@gmail.com'
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=['test@email.com'])

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', 'your@email.com', ['test@email.com'])

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

Community
  • 1
  • 1
darren
  • 18,845
  • 17
  • 60
  • 79
  • I've been trying to send email through my local server. But going through gmail works too I guess. Thanks – Luke Aug 02 '11 at 16:50
  • are you sure that EMAIL_HOST_USER includes gmail also, shouldn't it be just the 'username' ? – pranavk Jul 20 '12 at 09:29
  • 4
    moreover, it seems that send_mail syntax has slightly changed now in the latest django version, now four arguments are expected instead of 3. – pranavk Jul 20 '12 at 09:32
  • @pranavk Yes, Google specifies that using [your full email address](http://support.google.com/mail/bin/answer.py?hl=en&answer=13287) (including the @gmail.com) is required, most likely because they serve other domains through their Apps program. – Ryan Jenkins Nov 04 '12 at 21:36
  • It works from the shell, but when I use this code from view, It doesn't. Any ideas ?? – mariowise Sep 15 '14 at 22:28
  • for Digital ocean users, they blocks STMP ports by default http://stackoverflow.com/questions/27106336/smtp-mandrill-port-587-connection-timed-out – Julio Marins Mar 27 '16 at 06:51
46

First Create an Application specific password

  1. Visit your Google Account security page. And Click 2-step verification: enter image description here

  1. Click App passwords at Google Account security page: enter image description here

  1. Create an App, select Mail and give a name: enter image description here

  1. Note down the App Password: enter image description here

Then add the appropriate values to settings.py:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username@gmail.com'
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', 'your@email.com', ['toemail@email.com'],
     fail_silently=False)
Community
  • 1
  • 1
suhailvs
  • 20,182
  • 14
  • 100
  • 98
  • 1
    Sadly, app-specific passwords do not appear to be available for Google for Business gmail accounts. I get this error when I attempt to go directly to the App specific passwords page (https://security.google.com/settings/u/3/security/apppasswords) : The setting you are looking for is not available for your account. – randalv May 07 '16 at 18:01
  • here is updated [doc](https://github.com/suhailvs/upwork_temp/wiki/Create-a-Google-App-Specific-Password) – suhailvs Mar 26 '19 at 05:45
17

@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=['test@email.com'])

use

send_mail('test email', 'hello world', 'your@email.com', ['test@email.com'])

The first four arguments are required: subject, message, from_email, and recipient_list.

knite
  • 6,033
  • 6
  • 38
  • 54
4
  1. Enable pop3 in gmail settings.
  2. create application specific password for this django application. (http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833)
ameya1984
  • 87
  • 1
  • 6
3

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.

Rich Jones
  • 1,422
  • 1
  • 15
  • 17
1

In settings.py, Use smtp as backend and not console

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
0

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
user993563
  • 18,601
  • 10
  • 42
  • 55
  • 1
    I have used postfix instead of sendmail and same settings as above, but, the email is not sent. – toothie Oct 13 '14 at 04:40
  • @toothie I had the same problem until I realized I had not really installed postfix on my production server... – e18r Feb 26 '16 at 08:17
0

First setup the email backend variables in settings.py file.

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-username@gmail.com'
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', 'from@example.com', ['to1@example.com'], connection=connection, ) 
email.send() # Send the email
connection.close()
Praveen Kumar
  • 849
  • 8
  • 8