Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

PRACTICAL: 3

NAME : Shubham Annasaheb Waykar

PRN : 1941059

CLASS : TY COMP

BATCH : T4

AIM: Design a django project for “email sender”

THEORY

INTRODUCTION:
When you register on some websites, you get a mail from that company or institution? The
email would be, verification email or welcome email, account creation successful email or
thanks-regard email, etc. Let’s, illustration a Django project to send emails.

STARTING THE PROJECT


To initiate a project of Django on Your PC, open Terminal and Enter the following
command

django-admin startproject EmailProject

A New Folder with the name projectName will be created. To enter in the project using the
terminal enter command

cd EmailProject

Now let’s run the server and see everything is working fine or not. To run the server, type
the below command in the terminal.

python manage.py runserver

CREATING APP
Till now we have created the Django project and now we will create an app. Django is
famous for its unique and fully managed app structure. To create an app we need to go to
the directory containing manage.py and run the following command –

python manage.py startapp subscriber


➢ Settings.py:

Add your app name in settings.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'subscriber',
]

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'waykarshubham2025@gmail.com'
EMAIL_HOST_PASSWORD = 'password_here'

In the above code, EMAIL_HOST_USER = ‘km.srushti09@gmail.com’ and


EMAIL_HOST_PASSWORD = ‘srushti#123’ are the lines where you need to add
the sender’s mail id and password. km.srushti09@gmail.com’ and srushti#123’ are
just examples.

➢ Urls.py:
Create an urls.py in your app

from django.urls import path


from .views import myfun

urlpatterns = [
path("", myfun, name="home"),

➢ Views.py:
Create views.py in your app

from django.conf import settings


from django.core.mail import send_mail
from django.http import HttpResponse
def myfun(request):
subject = "Hello"
message = "Hello there, you have a new email"
email_from = settings.EMAIL_HOST_USER
recipient_list = ['km.srushti09@gmail.com',]
send_mail( subject, message, email_from, recipient_list )
return HttpResponse("<h1>!!! Email Sent !!!</h1>")

➢ urls.py:
Now add your apps urls in the project

from django.contrib import admin


from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path("", include("subscribe.urls"))

Now, let’s see what is happening here:


• subject refers to the email subject.
• message refers to the email message, the body of the email.
• email_from refers to the sender’s details. This takes the EMAIL_HOST_USER from
settings.py file, where you added those lines of code earlier.
• recipient_list is the list of recipients to whom the mail has to be sent that is, whoever
registers to your application they receive the email.
• send_mail is an inbuilt Django function that takes subject, message, email_from, and
recipient’s list as arguments, this is responsible to send emails.

You can send emails now. But if you are using Gmail, then the first time you make these
changes in your project and run, you might get SMTP error.
To correct that-

1-Go to the Google account registered with the sender’s mail address and select Manage
your account
2-Go to security section at the left nav and scroll down. In Less secure app access, turn on
the access. By default, it would be turned off.

OUTPUT:
Sender Gmail:

Receiver Gmail:

CONCLUSION:
Hence, in this practical we learnt to make email sender project in django

Sign of Teacher

You might also like