Gmail and Django

On July 02, 2007 in development, django, python, web

Did a bit of running around today to get Django sending email via Gmail. It’s simple once you figure it out.

If you’re running 0.96, upgrade to the latest development version or apply the patch from ticket #2897. 0.96 does not support TLS, which Gmail requires. Then add the appropriate values to settings.py:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'youremail@gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_PORT = 587

You can use the shell to test it:

>>> from django.core.mail import send_mail
>>> send_mail('Test', 'This is a test', to = ['youremail@somewhere.com'])

Edit: Bryan commented that send_mail is deprecated. Use EmailMessage instead:

>>> from django.core.mail import EmailMessage
>>> email = EmailMessage('Hello', 'World', to = ['youremail@somewhere.com'])
>>> email.send()

23 comments Add yours…

Anonymous, about 1 year ago

Much better then my hacked solution.

http://code.google.com/p/silassewell/wiki/Gmailer

Thanks for the info.

Nathan, about 1 year ago

@Anonymous:
Thanks for the link. Mine only works with SVN, so yours is a great workaround for those who don’t want to patch/upgrade.

Bryan, about 1 year ago

Very cool!

When I type help(send_mail) in the shell it says:

“NOTE: This method is deprecated. It exists for backwards compatibility.
New code should use the EmailMessage class directly.”

Any comments on that?

Nathan, about 1 year ago

@Bryan:
Ahh, I didn’t realize send_mail was deprecated. Thanks. I’ve updated the post.

Scott Johnson, about 1 year ago

Does anyone know which method we should be using in place of send_mail?

Nathan, about 1 year ago

@Scott:
You should use the EmailMessage method I write about at the end of my post.

Malcolm Tredinnick, about 1 year ago

To head any panic about the deprecation note (which isn’t even a Python warning yet): send_mail() will still be there in 1.0, at least. So it’s not like everybody needs to make a big rush away from using it. However, EmailMessage has more functionality (attachments, bcc recipients, text+HTML mail,…) and Django won’t be adding lots of new parameters to send_mail to keep up the parity between the two.

mezhaka, about 1 year ago

Do you know if it’s legal to use gmail for mass mailng this way? I’d like to make a mail list on my site and was wondering if I can do it this way withot violating some of the google’s rules?

Nathan, about 1 year ago

I don’t know offhand, mezhaka. You’re really better off using a source intended for mass mailing (my personal favorite is Campaign Monitor).

tercüme, 4 months ago

Your comment contains very useful information about all thank you rusça tercüman

ispanyolca tercüman, 4 months ago

Thanks for the link. Mine only works with SVN, so yours is a great workaround for those who don’t want to patch/upgrade…

Fabian, 4 months ago

If you use the patch 3 from the ticket #2897 is has to be EMAIL_TLS and not EMAIL_USE_TLS.

Frank Malo, 4 months ago

Now, I need to attach files in the mail message

charisma, 3 months ago

kewl. I implemented the send_mail thingy and now it works . BIG UP!
steve

Jon, 2 months ago

This was a little tricky at first but I was able to get it to work. I use the darwin ports version of python 2.5 and had to install py25-socket-ssl (“sudo port install py25-socket-ssl”).

  1. In my settings
    EMAIL_HOST = “smtp.gmail.com”
    EMAIL_HOST_USER = “SOME_NAME@gmail.com”
    EMAIL_HOST_PASSWORD = “YOUR_PASS”
    EMAIL_PORT = “587”
    EMAIL_USE_TLS = True
  1. In my view
    from django.core.mail import EmailMessage
    email = EmailMessage(“subject”, “body”, “from@example.com”, [“to@example.com”])
    email.send()
russell, about 1 month ago

In my understanding, i think u’d better setup ur own mail server ‘coz mailing via gmail takes a lot of time and it blocks the webserver(mod_python) process or wsgi/fcgi process.

Pupsor, about 1 month ago

I had tried this example, but gmail returned letters with this comment:

Technical details of permanent failure:
PERM_FAILURE: Message rejected. See http://mail.google.com/support/bin/answer.py?answer=69585 for more information.

maybe anybody know, what is it?

selbstbewusstsein, 29 days ago

all right. Now it works. bigthanks

Tal, 20 days ago

Thanks Nathan and Jon, you helped me ge this working in minutes!

J, 20 days ago

@mezhaka:
I believe that gmail has a limit of sending a 100 emails per 24h or something like that….

develope air force ones, 18 days ago

Reference: Very helpful, thanks!!

replica handbags, 5 days ago

thanks for that cool post

johnny, 5 days ago

thanks. got mine working in 30 seconds. will setup email server later. thanks again.

Post a comment