Welcome folks today in this post we will be talking about how to send test emails inside your django 3 application. We will be using gmail as our example server. You can take any server for your choice. All the steps are explained in depth and also a youtube video is given below to better understand the blog.
pip install django
Go to the directory where you need to create the project and then initialize this command to create a new project
django-admin startproject projectname
Creating Sample App
python manage.py startapp nameofapp
Registering Your App inside Project
Now go to the urls.py of the project and write these lines of code there
from django.contrib import admin from django.urls import path,include urlpatterns = [ path('admin/', admin.site.urls), path('',include('sendapp.urls')) ]
Here you need to replace sendapp with your app name. Basically in this line of code we are telling the django project that whenever django opens the home path then it should be redirected to a urls.py file which we will create inside our app folder.
Now inside your Django App folder create a new urls.py file and write this code inside it.
from django.urls import path from . import views urlpatterns = [ path('',views.index) ]
Here in this block of code we are configuring our urls. So the home path url will handle by a views.py file which is there inside our django app folder.
Now open your views.py folder and copy paste the below code.
from django.shortcuts import render # Create your views here. def index(request): return render(request,"send/index.html")
So Inside this code we are telling django when the request comes to the home path then return this template file which will be located inside send folder and there will be file index.html
So now inside your django app directory create a new templates directory and inside it create send directory and inside it create a index.html file and write the following code to it.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Django Send Email App</title> </head> <body> <h1>Email Sent Successfully</h1> </body> </html>
So inside this file we are just writing a basic html document and inside it we have a simple heading telling the user that the email was successfully sent.
Now we need to open the settings.py file of our project and write the basic settings which will be required by django to send the email to gmail such as the email address and password of the sender and also the port number etc. So open that file and copy paste the following code.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.gmail.com' EMAIL_USE_TLS = True EMAIL_PORT = 587 EMAIL_HOST_USER = "###youremailid###" EMAIL_HOST_PASSWORD = "###yourpassword####"
Enable Less Secure Apps in Gmail
Now after configuring all these details inside the settings.py of your project.
Now again open your views.py file and in that file update the code and paste the following code.
from django.shortcuts import render from django.core.mail import send_mail # Create your views here. def index(request): send_mail( "This is a sample Subject", "This is a test sample message", '####youremailid####', ['####recipentemailid####'], fail_silently=False) return render(request,"send/index.html")
Here in this block of code just before we are returning the index.html we are sending the email with the help of django send_mail function we are importing it at the very top from the core library of django and then we are using the send_mail function which takes the subject,body of message, and also email address of sender, and the recipient email address in an array. So you can define multiple email addresses to send multiple emails to multiple people at one time. And the last boolean parameter fail_silently is set to false.
Now the application is complete. Now head over to the cmd and execute the following command to start the django server
python manage.py runserver
After executing this command this will start your django server at port 8000 like below