Saturday, May 18, 2024
HomePythonImplementing Electronic mail Sending Performance in a Django App

Implementing Electronic mail Sending Performance in a Django App


Hello there people! Just lately I used to be doing a activity for fossasia which required me to make a Django internet app which allowed customers to submit their electronic mail addresses and obtain a welcome electronic mail in return. I used to be capable of full the challenge in a few hours. More often than not was spent in making the UI look good. I’m going to indicate you how one can simply make the same app.

For the sake of this tutorial we will probably be utilizing Gmail.

1.Setup the challenge

To start with we have to begin a django challenge. We are able to begin one on our Desktop by typing the next command within the terminal:

$ django-admin startproject gci_email

Now let’s cd into the newly created challenge and transfer on:

$ cd gci_email

2.Creating a brand new app

Now we have to make a brand new app. You are able to do that by operating the next command:

$ python handle.py startapp send_email

The above command creates a send_email listing inside our challenge with a few recordsdata in it.

3.Modify the challenge settings

Now let’s simply rapidly go forward and modify the challenge settings slightly bit. On this step we are going to add our app and all our electronic mail sending associated information to the settings.py file. Open the settings.py file which is offered within the gci_email folder and add the next content material to it:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'Your gmail electronic mail'
EMAIL_HOST_PASSWORD = 'Your gmail password'
DEFAULT_FROM_EMAIL = 'Your title'
DEFAULT_TO_EMAIL = 'Your electronic mail'

Within the INSTALLED_APPS part of your settings.py file that you must add the next entry to the INSTALLED_APPS checklist:

'send_email.apps.SendEmailConfig'

Now the settings.py file ought to comprise the next modified INSTALLED_APPS checklist:

INSTALLED_APPS = [
    'send_email.apps.SendEmailConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Let’s add the static recordsdata associated variables to our settings.py file as properly:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.be a part of(BASE_DIR, 'static'),
)

4.Creating urls

Modify the urls.py file in gci_email folder to look one thing like this:

from django.conf.urls import url, embody
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('send_email.urls')),
]

Now create a urls.py file in send_email folder as properly and add the next content material to it:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name="index"),
    url(r'^success', views.success, name="success"),
]

5.Including Views

Now it’s time to edit the views. Go to the views.py file in send_email listing and modify it to look one thing like this:

from django.shortcuts import render
from django.http import HttpResponse
import django
from django.conf import settings
from django.core.mail import send_mail


def index(request):
    return render(request, 'index.html')


def success(request):
    electronic mail = request.POST.get('electronic mail', '')
    information = """
Good day there!

I wished to personally write an electronic mail to be able to welcome you to our platform.
 We have now labored day and evening to make sure that you get one of the best service. I hope 
that you'll proceed to make use of our service. We ship out a publication as soon as a 
week. Just remember to learn it. It's often very informative.

Cheers!
~ Yasoob
    """
    send_mail('Welcome!', information, "Yasoob",
              [email], fail_silently=False)
    return render(request, 'success.html')

6.Including templates

Now we have to create a templates dir within the send_email folder and create 2 recordsdata in it. Specifically, index.html and success.html.

You want to create a type within the index.html file with an enter fild of title electronic mail. The success.html file doesn’t want any particular content material.

That’s it! Now you’ll be able to first create migrations, then apply them and at last run your challenge. Simply key within the following instructions:

$ python handle.py makemigrations
$ python handle.py migrate
$ python handle.py runserver

I’ve deployed a pattern app over right here and the code for this challenge is offered on GitHub.

Cheers!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments