DEV Community

Cover image for How to Send Email in Django | Django Email Project – DevDuniya
Dev Duniya
Dev Duniya

Posted on • Originally published at devduniya.com

How to Send Email in Django | Django Email Project – DevDuniya

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.

python --version
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
Table of Contents Hide
1 Step:-1
2 Step:-2
3 Step:-3
4 Step:-4
5 Step:-5
6 Step:-6
7 Conclusion:
7.1 Follow me to receive more useful content:

Step:-1

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.

Step:-2

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'
Enter fullscreen mode Exit fullscreen mode

]

Step:-3

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',
'from@gmail.com',
['to@gmail.ccom'],
fail_silently=False,
)
return render(request, 'index.html')

Step:-4

Now make a folder in mailapp with ‘templates’ name, then create a file name index.html. Past the below code in index.html.

Read Continue>>>

Thankyou

Top comments (0)