Julia Ebert
Julia Ebert

Reputation: 1623

Django email not sending

I am trying to send email through Django as part of django-userena, but I am not able to get email to send at all. In my settings, I have:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'mypassword'

I try to send an email from the Django console with:

from django.core.mail import EmailMessage
email = EmailMessage('Mail Test', 'This is a test', to=['[email protected]'])
email.send()

It hangs on the send command and doesn't actually send the email. If I stop the command, I get this traceback:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/myuser/Copy/Projects/Programming/myproject/venv/local/lib/python2.7/site-packages/django/core/mail/message.py", line 274, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/myuser/Copy/Projects/Programming/myproject/venv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 87, in send_messages
    new_conn_created = self.open()
  File "/home/myuser/Copy/Projects/Programming/myproject/venv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 48, in open
    local_hostname=DNS_NAME.get_fqdn())
  File "/usr/lib/python2.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 312, in connect
    (code, msg) = self.getreply()
  File "/usr/lib/python2.7/smtplib.py", line 356, in getreply
    line = self.file.readline()
  File "/usr/lib/python2.7/socket.py", line 447, in readline
    data = self._sock.recv(self._rbufsize)

Any help on why this isn't going through?

Upvotes: 5

Views: 3385

Answers (1)

e.thompsy
e.thompsy

Reputation: 1377

I had this same problem. I am using Django 1.6. It turns out I needed to use SSL to send email via gmail. So I used this handy package: https://github.com/bancek/django-smtp-ssl

$ pip install django-smtp-ssl

Then settings.py should have this:

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'YOUR_PASSWORD'

Of course, if you are using Django 1.7 then you can just add EMAIL_USE_SSL = True to settings.py and use the default backend.

Upvotes: 7

Related Questions