How to Send Email in Python Django ?

Published On: 25/06/2022 | Category: Django Python

HI Dev,

Are you looking for example of how to send email in python django. This post will give you simple example of how to send email in python django app. it's simple example of how to send email in python django example. I explained simply step by step how to send email in python django framework. Let's see bellow example google smtp send email using python django example.

Let's see bellow example google smtp send email using python django example.

Step 1 : Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject exampleapp
Step 2 : Create a App
python3 startapp sendmail

Next, you need to add it in the file as follows:

Step 3 : Database Setup

Next step, we will modify the file and update the smtp email backends settings to configure:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'bhaveshs.aa*******'
Step 4 : Set Up an App Password in Gmail

In this section we will create EMAIL_HOST_PASSWORD setting you’ll need to create the app password from your personal email address.

So, you can get a Gmail app password with the steps below. Note that if you’re using an existing account and have enabled 2-step verification:

  1. Create or Login into a Gmail account
  2. Enable two-factor-authentication, as it’s required to get an app password.
  3. Now you have two-factor authentication enabled, it’s time to get an app password. You can do this by going to the security section of your google account, scrolling down to the Signing in to Google section, and clicking on App passwords.

Once you’re in, click on select app, where you’ll choose a custom name for that app password — such as “Django Send Email” — then click on GENERATE.

A new window will show up with a sixteen-character password. Copy it, because we’ll need it to configure our Django project.

Step 5 : Creating the Views

In this step, we need to create the views for performing fetch record to the database.Open the sendmail/ file and add:

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from .forms import EmailForm
from django.conf import settings
from django.core.mail import send_mail

# Create your views here.

def sendMail(request):
    'Testing Mail From Django',
    'Welcome to',
    '[email protected]',
    ['[email protected]'],
    return HttpResponse('Mail Successfully Sent!')
Step 6 : Creating Urls

In this section, we’ll create the urls to access our views.Go to the sendmail/ file and update it as follows:

from django.urls import path
from sendmail import views

urlpatterns = [
    path('send-email', views.sendMail),

Next, we will require the modify the your root preoject folder lets update the file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('sendmail.urls')),
Step 7 : Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the url address bar with a web browser.


I Hope It will help you....