How to Send Email in Python Django ?
Are you looking for example of how to send email in python django. This post will give you simple example of how to send email in python django app. it's simple example of how to send email in python django example. I explained simply step by step how to send email in python django framework. Let's see bellow example google smtp send email using python django example.
Let's see bellow example google smtp send email using python django example.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 exampleappStep 2 : Create a App
python3 manage.py startapp sendmail
Next, you need to add it in the settings.py file as follows:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'sendmail', ]Step 3 : Database Setup
Next step, we will modify the settings.py file and update the smtp email backends settings to configure:settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.gmail.com' EMAIL_USE_TLS = True EMAIL_PORT = 587 EMAIL_HOST_USER = 'email@example.com' EMAIL_HOST_PASSWORD = 'YOUR_APP_PASSWORD'Step 4 : Set Up an App Password in Gmail
In this section we will create EMAIL_HOST_PASSWORD setting you’ll need to create the app password from your personal email address.
So, you can get a Gmail app password with the steps below. Note that if you’re using an existing account and have enabled 2-step verification:
- Create or Login into a Gmail account
- Enable two-factor-authentication, as it’s required to get an app password.
- Now you have two-factor authentication enabled, it’s time to get an app password. You can do this by going to the security section of your google account, scrolling down to the Signing in to Google section, and clicking on App passwords.
Once you’re in, click on select app, where you’ll choose a custom name for that app password — such as “Django Send Email” — then click on GENERATE.
A new window will show up with a sixteen-character password. Copy it, because we’ll need it to configure our Django project.
Step 5 : Creating the Views
In this step, we need to create the views for performing fetch record to the database.Open the sendmail/views.py file and add:sendmail/views.py
from django.shortcuts import render from django.http import HttpResponse, JsonResponse from .forms import EmailForm from django.conf import settings from django.core.mail import send_mail # Create your views here. def sendMail(request): send_mail( 'Testing Mail From Django', 'Welcome to Tuts-Station.com', '[email protected]', ['[email protected]'], fail_silently=False, ) return HttpResponse('Mail Successfully Sent!')Step 6 : Creating Urls
In this section, we’ll create the urls to access our views.Go to the urls.py sendmail/urls.py file and update it as follows:sendmail/urls.py
from django.urls import path from sendmail import views urlpatterns = [ path('send-email', views.sendMail), ]
Next, we will require the modify the urls.py your root preoject folder lets update the file.autocomplete/urls.py
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('sendmail.urls')), ]Step 7 : 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 manage.py runserver
Next, go to the url address bar with a web browser.
I Hope It will help you....