Django sending email
First Create an Application specific password
- Visit your Google Account security page. And Click 2-step verification:
- Click
App passwords
at Google Account security page:
- Create an
App
, selectMail
and give a name:
- Note down the
App Password
:
Then add the appropriate values to settings.py:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
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', '[email protected]', ['[email protected]'],
fail_silently=False)
Are you trying to use a gmail account? Maybe try this then:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
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=['[email protected]'])
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', '[email protected]', ['[email protected]'])
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
- 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)
@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=['[email protected]'])
use
send_mail('test email', 'hello world', '[email protected]', ['[email protected]'])
The first four arguments are required: subject, message, from_email, and recipient_list.