DEV Community

loading...
Cover image for  Send emails with Django and Gmail   
 , a better way

Send emails with Django and Gmail , a better way

abderrahmanemustapha profile image Toumi Abderrahmane ・Updated on ・2 min read

In this post I want to talk about how can we send emails using Django and Gmail, I read a lot of articles about this but none of them is working for me, this is my way of doing this

Setup Django πŸ’š

first let's install django

 pip install django
Enter fullscreen mode Exit fullscreen mode

start a django project

django-admin startproject send_gmail
Enter fullscreen mode Exit fullscreen mode

start a django app

cd send_gmail
python manage.py  startapp send
Enter fullscreen mode Exit fullscreen mode

add the app to settings.py installed app

# send_gmail/settings.py
INSTALLED_APPS = [
...
send,
]
Enter fullscreen mode Exit fullscreen mode

migrate and create a super user

python manage.py  migrate
python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

check this post if you want to know more about how to create a virtual environment , or how to install Django and start your project

The Gmail part βœ‰

now you need to create a Gmail account and then click on Manage your google account
manage account screen shot

now click on the Security tab

image account security

make sure to enable two steps verification

enter image description here

now click on App passwords

enter image description here

you have to type your password again

Confirm your password

click on select app choose *** other (Custome Name) *** and give a name to you app

Create a new app

the last step click on generate and Gmail will generate a key or an app password make sure to copy this key or save it in a text file

Create a new app

Edit your settings.py file

#gmail_send/settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'yoorusername@gmail.com'
EMAIL_HOST_PASSWORD = 'key' #past the key or password app here
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'default from email'
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰πŸŽ‰ now you are ready to send emails with Django and Gmail in productionπŸŽ‰πŸŽ‰

For further exploration

you can use SendGrid, Mailgun, Sendinblue... for your apps too
don't forget to put your key in a .env file

Discussion (17)

pic
Editor guide
Collapse
jeroen1205 profile image
Jeroen Jacobs

I do it a bit differently: I run PostFix on my servers, which is configured to use an external SMTP server as forwarder (can be GMail, SES, ...). Then I let my app deliver to my local SMTP server.

This has a few advantages:

  • App is no longer coupled to GMail settings
  • The most important one: mails will be queued on local server, in case remote SMTP goes down or is not available. Most SMTP servers already handle automatic retries, and dead mail queues out of the box.

If you do it within the Django app, I would suggest to add some message queue stuff (Celery) so SMTP calls don't block your app, or retry in case of failure.

Collapse
abderrahmanemustapha profile image
Toumi Abderrahmane Author

that's great thank you will try to implement this

Collapse
jordanahaines profile image
Jordan Haines

Great post! Adding on to your note RE further exploration. For projects that outgrow sending email via a single Gmail account, check out github.com/anymail/django-anymail. After playing with a few packages to interact with Mandrill/Sendgrid/SES, I've found Anymail to be the most flexible.

Collapse
abderrahmanemustapha profile image
Toumi Abderrahmane Author

thank you I didn't know about django-anymail i think that I will use it with my next project

Collapse
ioi2908 profile image
maen

why it doesn't work for me?

Collapse
abderrahmanemustapha profile image
Toumi Abderrahmane Author

can you tell me what is the problem that your facing exactly so maybe I can help you

Collapse
namibnat profile image
Vernon Swanepoel

Much appreciated, thank you.

Collapse
abderrahmanemustapha profile image
Toumi Abderrahmane Author

thank you ? πŸ€—

Collapse
josejaviercb profile image
Javier Contreras

Hello, thanks for sharing your knowledge, I have a question, I have a contact form, I am using a gmail account to send, but I want the email of the person who writes to appear as the sender and not my gmail account, it is can you do this?

Collapse
aju profile image
Aju Tamang

aweosome

Collapse
abderrahmanemustapha profile image
Collapse
charly2009 profile image
charly2009

thanks very much

Collapse
ioi2908 profile image
maen

why it doesnt work for me?

Collapse
barelysaved profile image
Susie

My contact form is still throwing an error.

Collapse
abderrahmanemustapha profile image
Toumi Abderrahmane Author

can you send the full error please on my Twitter twitter.com/Abderrahmaneend

Collapse
fanoftheauthor profile image
S. E. Newton

I can't login DEV with my email address but I did follow you on Twitter so I can send the error.

Thread Thread
abderrahmanemustapha profile image
Toumi Abderrahmane Author

thats great , im waiting for ya :)