Dave Perrett

Sending Django Email With Gmail

django, programming, python, quickie, web

After spending a few hours struggling to get postfix to work with django on my local dev machine, I gave up and decided to use Gmail . To do it, add the following to your settings.py :

1
2
3
4
5
EMAIL_USE_TLS       = True
EMAIL_HOST          = 'smtp.gmail.com'
EMAIL_HOST_USER     = 'username@gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT          = 587

(Obviously, substituting your own email and password)

You can also use an address at your own domain if you set the DNS records to use google apps.

To test if it’s working :

1
2
3
4
> python manage.py shell
>>> from django.core.mail import EmailMessage
>>> email = EmailMessage('Mail Test', 'This is a test', to=['test@your-address.com'])
>>> email.send()