Email in Django

How to Send Email in Django – ProgramSpeaker

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 ProviderSMTP Server Host
Gmailsmtp.gmail.com
Outlook/Hotmailsmtp-mail.outlook.com
Yahoosmtp.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;
    1. Login to Gmail
    2. Go to myaccount.google.com
    3. Enable 2-factor authentication(as it’s required to get an app password)
    4. Go to the security section and scroll down to the signing into google section
    5. 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.

Share this:

16 thoughts on “How to Send Email in Django – ProgramSpeaker”

  1. 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.

  2. 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.

  3. 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!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top