DEV Community

Cover image for Sending Emails in Django
Madhuban Khatri
Madhuban Khatri

Posted on • Updated on

Sending Emails in Django

Configuration for Sending Emails in Django:-

Watch my video on YouTube

settings in Your Gmail Account

Alt Text

  • Open your email account in Browser
  • go to 'Manage your google account'
  • go to 'Security' tab in left side bar
  • Turn ON the Toggle Button in 'Less Secure App Access' option in Security tab.

Add Some line of Code in this file.

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'




EMAIL_HOST_USER = 'your email id'

Enter fullscreen mode Exit fullscreen mode


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

urlpatterns = [
    path('', include('main.urls')),
Enter fullscreen mode Exit fullscreen mode


from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('send_mail/', views.sendMail, name='send_mail')
Enter fullscreen mode Exit fullscreen mode

from django.shortcuts import render, redirect
from django.conf import settings
from django.core.mail import send_mail
from django.contrib import messages
# Create your views here.
def home(request):
    return render(request, 'home_page.html')

def sendMail(request):
    if request.method == 'POST':
        sender = settings.EMAIL_HOST_USER
        receiver = request.POST['receiver']
        subject = request.POST['sub']
        content = request.POST['content']

        mail = send_mail(subject, content, sender, [receiver], fail_silently=False)
        if mail:
            messages.success(request, 'Email has been sent.')
            return redirect('home')
            return HttpResponse('message not sent')
        return redirect('home')
Enter fullscreen mode Exit fullscreen mode


<!doctype html>
<html lang="en">
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

    <title>Sending Email in Django</title>
  <body class="bg-dark text-light">
    <div class="container my-3" style="width: 500px;">

      <h2 class="alert alert-success">Sending Email in Django</h2>
      {% if messages %}
      {% for message in messages %}
      <div class="alert alert-dark alert-dismissible fade show my-4" role="alert">
      <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
      {% endfor %}
      {% endif %}

    <form action="{% url 'send_mail' %}" method="post">
      {% csrf_token %}
      <div class="mb-3">
        <label for="exampleInputEmail1" class="form-label"><b>To</label>
        <input type="email" class="form-control" name="receiver" id="exampleInputEmail1" aria-describedby="emailHelp">
      <div class="mb-3">
        <label for="subject" class="form-label"><b>Subject</label>
        <input type="text" class="form-control" name="sub" id="subject">
      <div class="mb-3">
        <label for="content" class="form-label">Body</label>
        <textarea class="form-control" name="content" style="height: 250px;"></textarea>


      <button type="submit" class="btn btn-primary">Send</button>


    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

segestic profile image

Nice work. Keep it up!

cpoonam47 profile image

Wonderfull, Nice Work , I want to add my project file