How to Send Email with Multiple File Attachment in Python Django?

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

This article will provide some of the most important example how to send email with multiple file attachment in python django. you can see send email multiple file attachment in python django example. you can see send multiple files attachment mail using django. We will look at example of how to django send email with attachment.

Let's see bellow example I explained simply about sending emails with attachment in django.

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 : Setup Email Configuration

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 : Create a FormClass

First of all in this step we will create a new file called and create a Django form you need to use Django Form Class

from django import forms

# creating a form
class EmailForm(forms.Form):
    email = forms.EmailField()
    subject = forms.CharField(max_length=100)
    attach = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
    message = forms.CharField(widget=forms.Textarea(attrs={'rows': 4}))
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.http import HttpResponseRedirect
from django.shortcuts import render
from django.views import View
from django.core.mail import EmailMessage
from django.conf import settings
from .forms import EmailForm

class EmailAttachementView(View):
    form_class = EmailForm
    template_name = 'emailattachment.html'

    def get(self, request, *args, **kwargs):
        form = self.form_class()
        return render(request, self.template_name, {'email_form': form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, request.FILES)

        if form.is_valid():
            subject = form.cleaned_data['subject']
            message = form.cleaned_data['message']
            email = form.cleaned_data['email']
            files = request.FILES.getlist('attach')

                mail = EmailMessage(subject, message, settings.EMAIL_HOST_USER, [email])
                for f in files:
                    mail.attach(,, f.content_type)
                return render(request, self.template_name, {'email_form': form, 'error_message': 'Sent email to %s'%email})
                return render(request, self.template_name, {'email_form': form, 'error_message': 'Either the attachment is too big or corrupt'})

            return render(request, self.template_name, {'email_form': form, 'error_message': 'Unable to send email. Please try again later'})
Step 6 : Creating the Templates

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

{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Send Multiple Files Attachment Mail using Django</title>
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
    <div class="container mt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-9">
                <div class="card">
                    <div class="card-header">
                        <h4>How to Send Email with Multiple File Attachment in Python Django?</h4>
                    <div class="card-body">
                        {% if error_message %}
                            <div class="alert alert-success" role="alert">
                        {% endif %}
                        {% if email_form %}
                            <form method="POST" action ="" enctype="multipart/form-data">
                                {% csrf_token %}
                                <button type="submit" class="btn btn-success">Send Email</button>
                        {% endif %}
Step 7 : 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('send-email', EmailAttachementView.as_view(), name='emailattachment')

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