Here we’ll walk through how to send emails using Django. In most customer-driven web applications, there may be a need to send an email. Follow this tutorial only after setup your project. And although you can refer to the Django-documentation to knowing more about sending emails in Django.
Step 1: Update the settings.py file
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "sender's email-id"
EMAIL_HOST_PASSWORD = "app password associated with above email-id"
In the above code,
- “EMAIL_BACKEND” is the backend of our Django project that will use to connect with the SMTP server.
- “EMAIL_HOST” settings refer to the SMTP server domain you will be using. This fully depends on your mail provider. Here we have some common providers and their SMTP server hosts.
Email Provider | SMTP Server Host |
Gmail | smtp.gmail.com |
Outlook/Hotmail | smtp-mail.outlook.com |
Yahoo | smtp.mail.yahoo.com |
- “EMAIL_USE_TLS” is a security protocol used across the web to encrypt the communication between web apps(Django) and servers(SMTP server). TLS – Transport Layer Security
- “EMAIL_PORT” this setting must be set to 587 because it is the default port for most SMTP servers.
- “EMAIL_HOST_PASSWORD” is the app password for the email id. To configure the app password please go through the link create-app-password. It includes;
- Login to Gmail
- Go to myaccount.google.com
- Enable 2-factor authentication(as it’s required to get an app password)
- Go to the security section and scroll down to the signing into google section
- Click on App password and generate(Before leaving take a copy of the password)
- “EMAIL_HOST_USER” is the sender’s email id
You may like: Django V/S Flask, What makes them different?
Step 2: Update the views.py file
Now we can move to the views.py file. At the top add the following lines of code
from django.conf import settings from django.core.mail import send_mail
Next, paste the below code on your function where ever you want to send an email.
subject = 'Welcome to ProgramSpeaker' message = f'Hi user, thank you for going with programspeaker.' email_from = settings.EMAIL_HOST_USER recipient_list = ['recipient_email@gmail.com', ] send_mail( subject, message, email_from, recipient_list )
Here;
subject refers to the email subject/title
message refers to the content/body of the email
email_from is the sender’s details. That taken from EMAIL_HOST_USER in the settings.py file
recipient_list is the list of email recipients
send_mail is the inbuilt Django function to send mails
Now you can run your Django application along with the email function. Then the user will receive mail from the email account you mentioned.
I got thiѕ web site from my friend who informed me
regаrding this site and at the moment this timе I am visiting this web site and readіng very informative content at this time.
Thank you for your valuable feedback
I used to be able to find good advice from your blog posts.
We stumbled over here coming from a different page and thought I might check things
out. I like what I see so i am just following you. Look forward to exploring your web
page repeatedly.
Thank you for your valuable feedback, will be coming up with new topics.
Good site you’ve got here.. It’s difficult to find excellent writing like yours nowadays.
I truly appreciate individuals like you! Take care!!
Thank you for your valuable feedback.
Hi, the whole thing is going nicely here and ofcourse every one is sharing data, that’s in fact excellent, keep up writing.
Thank you for your valuable feedback.
Right away I am ready to do my breakfast, after having my breakfast coming over again to read additional news.
Thank you, Keep in touch with us.
Hi there, I discovered your web site via Google at the
same time as looking for a similar topic, your website got
here up, it appears to be like good. I’ve bookmarked it in my
google bookmarks.
Hello there, simply changed into aware of your weblog via Google, and found that
it is really informative. I’m going to be careful for brussels.
I will be grateful when you continue this in future.
Numerous other people will probably be benefited out of your writing.
Cheers!
Thank you for your valuable feedback.
Excellent, what a blog it is! This webpage gives
useful data to us, keep it up.
Thank you for your valuable feedback, will be coming up with new topics.
I enjoy looking through a post that can make people think.
Also, thank you for permitting me to comment!