DEV Community

Cover image for  Send emails with Django and Gmail   
 , a better way
Abderrahmane Mustapha
Abderrahmane Mustapha

Posted on • Edited on

Send emails with Django and Gmail , a better way

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

Latest comments (25)

Collapse
 
andemusgbj profile image
Albert Gubanja • Edited

Hello,
I want to send email contact with django,
But after all config in settings.py, I am facing this issue:

SMTPSenderRefused at /contacts
(530, b'5.7.0 Must issue a STARTTLS command first. m42-20020a05600c3b2a00b003cf47556f21sm7462655wms.2 - gsmtp', 'webmaster@localhost')

Can anyone help me?
Here is my settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'albertandemsj@gmail.com'
EMAIL_PASSWORD = 'key'
EMAIL_PORT = 587
EMAIL_USE_TSL = True
EMAIL_USE_SSL = False
Enter fullscreen mode Exit fullscreen mode

And my sending email view.py

if request.method == 'POST':
        name = request.POST.get('name')
        email = request.POST.get('email')
        subject = request.POST.get('subject')
        message = request.POST.get('comments')

        data = {
            'name':name,
            'email': email,
            'subject': subject,
            'message': message
        }

        message = ''' 
        New Message : {}

        From : {}
        '''.format(data['message'], data['email'])

        send_mail(data['subject'], message,'' ,['qweaverinfo@gmail.com'])
        return render(request, 'layouts/auth-signup-success.html', locals())

    return render(request, 'layouts/contact.html', locals())
Enter fullscreen mode Exit fullscreen mode
Collapse
 
iliyahajjar profile image
iliya-hajjar

I did all the steps however when using send_mail() it returns 1 but I get no email in my inbox.

Collapse
 
ruslanskira profile image
Ruslan Skira

Only your tutorial helps. After 3 hours googling such easy topic. Thank you very much.

Collapse
 
vmondo91 profile image
vmondo91 • Edited

Only in production?
I tried it in testing mode and it doesn't work but it is working on mailtrap

Collapse
 
abdelhamedabdin profile image
AbdelhamedAbdin

it is a very helpful tutorial
thx for help :)

Collapse
 
oussama_he profile image
Oussama Heloulou

Should this work properly and don't cause any problems if I send 30 emails per day?

Collapse
 
giftedstan profile image
giftedstan

yes, you can send up to 2000 mails per day

Collapse
 
oussama_he profile image
Oussama Heloulou

Thank you for your reply.
I used it to send emails from the website to users, but it stopped working, (it works if I try to send from my PC, but doesn't work from the server)

Collapse
 
charly2009 profile image
charly2009

thanks very much

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
 
maen profile image
Ruheza, NS

why it doesn't work for me?

Collapse
 
abderahmane profile image
Abderrahmane Mustapha

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

Collapse
 
maen profile image
Ruheza, NS

why it doesnt work for me?

Some comments may only be visible to logged-in visitors. Sign in to view all comments.