In this article, we are going to see How to Send Email in Django. Sending an email is quite simple in Django and so, We are going to create such kind of Django Project.
You will have to do the first complete setup for this project then follow the steps given below. To follow along with this tutorial, you need a Gmail account, prior knowledge of Django is required, and Basic HTML. If you don’t have a Gmail account then follow this link to create a new Gmail account or google account.
Before starting the project, Download python programming from their official website and install it in your system. Once python is installed then you can simply check its version by command prompt. Enter the below command in CMD.
After downloading the python programming, Now install Django with the help of Command Prompt. Enter the command given below:-
pip install django python -m django --version
Now we will first create a project then we will create an app for that project. Do follow the below steps :
django-admin startproject mailproject cd mailproject python manage.py startapp mailapp python manage.py migrate python manage.py runserver
After these steps, Now your Django server has been started.
Now you can navigate to http://127.0.0.1:8000/ using your browser to see the default Django homepage.
Add your app in the settings.py file.
# Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', #add app 'mailapp' ]
Now in the same file settings.py, below all code add some SMTP configuration code.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = "smtp.gmail.com" EMAIL_PORT = 587 EMAIL_HOST_USER = "" #add here your actual mail id EMAIL_HOST_PASSWORD = "" #password of your mail id that you have added above EMAIL_USE_TLS= True
Step:-4 Now add some code into your views.py file of mailapp folder. So that it will look like this:
from django.shortcuts import render from django.core.mail import send_mail # Create your views here. def mailfunction(request): send_mail( 'Title of mail', 'Messagee', 'firstname.lastname@example.org', ['email@example.com'], fail_silently=False, ) return render(request, 'index.html')
Now make a folder in mailapp with ‘templates’ name, then create a file name index.html. Past the below code in index.html.
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Congratulations!!!</h2> <div class="alert alert-success"> <strong>Success!</strong> Email Sent!!!! </div> </div> </body> </html>
Update your urls.py file of mailproject folder.
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('emailapp.urls')), ]
Create a new file in mailapp folder with the urls.py file name. Remember one thing, this urls.py file is in mailapp folder not in mailproject folder. Now add some code into the urls.py file.
from django.urls import path from . import views urlpatterns = [ path('', views.mailfunction, name="mailfunction"), ]
Now Run the Django Server:-
So we have successfully created a simple Django Project. We have covered how to send an email in the django framework.
If you have any queries related to this article, then you can ask in the comment section, we will contact you soon, and Thank you for reading this article.
Follow me to receive more useful content: