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?
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.
In most cases, you can send email using django. core. mail. send_mail() .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With