How to Send Email to Multiple User using Django?

Published On: 11/07/2022 | Category: Django Python

This simple article demonstrates of how to send email to multiple user using django python. This article will give you simple example of how to send email to multiple user using django admin. we will help you to give example of how to send email to multiple user using django example . you'll learn how to send email to multiple user using django python.

Let's see bellow example I explained simply about how to send email to multiple user using django python.

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 : Email Configuration 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 : 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.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.core.mail import send_mail

class usersList(View):

    def get(self,request):
        users= get_user_model().objects.all()
        context = {
        return render(request, 'usersList.html',context)

    def post(self,request,*args,**kwargs):
        if request.method == 'POST':
            users_ids = request.POST.getlist('ids[]')

            for id in users_ids:
                user = User.objects.get(pk=id)
                    'Mail From',
                    'Welcome to',
                    '[email protected]',
            return redirect('users')
Step 5 : Creating the Templates

Next, open the sendmail/templates/usersList.html file and the add:

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Django Send Email to Multiple Users -</title>
    <link rel="stylesheet" href="" />
    <link rel="stylesheet" href="">
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <div class="container mt-5 pt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-8">
                <div class="row">
                    <div class="col-md-9 p-0">
                        <h5><b>Django Send Email to Multiple Users - <span class="text-danger"></span></b></h5>
                    <div class="col-md-3 text-right mb-2 p-0">
                        <button type="button" class="btn btn-primary send-mail btn-sm" disabled="disabled"> <i class="fa fa-share"></i> Send Mail</button>
                    <div class="col-md-12 success-mail p-0" style="display: none;">
                        <div class="alert alert-success">
                          Sent Mail Successfully.
                <div class="row">
                    <table class="table table-bordered">
                                <th><input type="checkbox" value="1" name="user-all" class="user-all"></th>
                            {% csrf_token %}
                            {% for user in users %}
                                <td><input type="checkbox" name="ckeck_user[]" class="ckeck_user" value="{{ }}"></td>
                                <td>{{ user.username}}</td>
                                <td>{{ }}</td>
                            {% endfor %}

    $('.user-all').change(function (e) {
        var value = $('.user-all:checked').val();
        if (value == 1) {

    $(".ckeck_user").change(function () {
        if ($(".ckeck_user:checked").length > 0) {

    $('.send-mail').click(function (e) {
        var ids = [];
        var token = $('input[name="csrfmiddlewaretoken"]').val();

            ids[i] = $(this).val();

        if (ids != '') {
            $(this).attr("disabled", true);
            $(this).html('<i class="fa fa-spinner fa-spin"></i> Send Mail');
                url: '{% url "users" %}',
                type: 'POST',
                data: {
                    csrfmiddlewaretoken: token,
                success: function (data) {
                    $('.ckeck_user, .user-all').prop('checked',false);
                    $('.send-mail').attr("disabled", false);
                    $('.send-mail').html('<i class="fa fa-share"></i> Send Mail');
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.views import EmailAttachementView

urlpatterns = [
    path('users', usersList.as_view(), name='users'),

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