Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 282
Julia Ebert Avatar asked Sep 01 '14 23:09

Julia Ebert


People also ask

Why are my emails not sending through?

The error could be a general network issue, and you may want to try again in a few minutes. If the issue persists, you could have a DNS issue. If DNS is not working, you cannot send emails. Of course, it could be something simple – like an incorrectly spelled domain name.

How do I send an email using Django?

In most cases, you can send email using django. core. mail. send_mail() .

What is SMTP in Django?

Understanding SMTP An SMTP server always has a unique address, and a specific port for sending messages, which in most cases is 587. We'll see how the port is relevant while sending emails with Django. Since we'll be using Gmail, the address we'll be working with is smtp.gmail.com , and the port will be 587.

What is SMTP library in Python?

Source code: Lib/smtplib.py. The smtplib module defines an SMTP client session object that can be used to send mail to any internet machine with an SMTP or ESMTP listener daemon.


1 Answers

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.

like image 184
e.thompsy Avatar answered Sep 21 '22 07:09

e.thompsy