Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Django IntroductionChapter 6. Deploy Django App

Email Setting – SendGrid

Email Setting – SendGrid

Configuring Email with SendGrid for Django

In the production stage, directly using your own Gmail or another email address may not be appropriate from a security and service credibility point of view. Also, when your service becomes large, your sent-email box can be full of machine-generated emails. There are third-party services that handle the machine-generated emails on behalf of your email server, such as SendGrid, Amazon SES, or Mailtrap. In our case example, we use SendGrid.

There are four key steps for SendGrid setup for Django.

  1. Sign up on SendGrid
  2. Create an API Key
  3. Create a sender
  4. Install django-sendgrid and update the Django settings

If you don't want to have the SendGrid brand name in the verification email sent by your Django app, you'll need to manage the sender authentication process on top of the four steps above.

Sign up SendGrid

Go to the SendGrid website and click on Start for free.

Sign up SendGrid: Step 1

Type your email and password.

Sign up SendGrid: Step 2

You also need to fill in your full name and some additional information to create an account.

Create an API Key

After you create an account, you need to create an API key that will be used in the Django setting later. Go to the API Key page under Settings in the left menu. Click on the Create API Key button.

SendGrid Create an API Key: Step 1

Select Full Access unless you want to customize the setting.

SendGrid Create an API Key: Step 2

As a new API Key is shown only once, you need to make sure you copy and save it somewhere.

SendGrid Create an API Key: Step 3

Create a sender

You need to register an email address that you want to use in the Django app.
Go to Senders under the Marketing dropdown list. Click on the Create New Sender button on the top right.

SendGrid Create a sender: Step 1

Add your sender information and save it. A verification email will be sent to the registered email address. Once you verify your email address, you'll get a confirmation message.

SendGrid Create a sender: Step 2

And, you'll see that your email has been registered.

SendGrid Create a sender: Step 3

Install django-sendgrid and update the Django settings

Once you get an API key and register a sender, you need to install the Django SendGrid library and update Django settings.

Install django-sendgrid

Add django-sendgrid-v5 in the requirements file.

requirements.txt
Django==4.1.7
django-crispy-forms
crispy-bootstrap5
django-allauth
django-environ
gunicorn
psycopg2-binary
django-sendgrid-v5

and run the pip command.

Command Line - INPUT
pip install -r requirements.txt

Update the Django settings file and .env file

As we won't use Gmail in the production stage, comment out or delete the settings.

Add the four settings for SendGrid.

config/production.py
 :
STATIC_ROOT = '/usr/share/nginx/html/static'

# SendGrid
EMAIL_BACKEND = 'sendgrid_backend.SendgridBackend'
SENDGRID_API_KEY = env('SENDGRID_API_KEY')
DEFAULT_FROM_EMAIL = env('DEFAULT_FROM_EMAIL')
SENDGRID_SANDBOX_MODE_IN_DEBUG = False
SENDGRID_TRACK_CLICKS_PLAIN = False

If SENDGRID_SANDBOX_MODE_IN_DEBUG is True, actual emails won't be delivered.
SENDGRID_TRACK_CLICKS_PLAIN is for URL tracking. If this setting is True, you'll see a long URL in the verification email. In our case, change it to False.

Actual information for the API key and default email should be saved in the .env file.

.env
DEFAULT_FROM_EMAIL=info@example.com
SENDGRID_API_KEY=XXXXXXXXXXXXXXXXXXXX

For more details, please check the official document.

django-sendgrid-v5

Check if the verification email is working in the production environment

Restart the application server and check the status.

Command Line - INPUT
 sudo systemctl restart project_d
systemctl status project_d
Command Line - RESPONSE
● project_d.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/project_d.service; disabled; vendor preset: enabled)
Active: active (running) since Mon 2023-04-24 13:30:20 UTC; 1s ago
TriggeredBy: ● project_d.socket

If the app is running properly, try to sign up. You'll see that the verification email has been successfully sent.

Email verification message

And you'll receive an email like the one below. You can see that the email was sent via sendgrid.net.

SendGrid verification email example

Sender Authentication Setup

If you don't want to have the SendGrid name in the verification email sent by SendGrid, you need to go through the sender authentication process.

You can set it up through Sender Authentication under the Settings dropdown menu.

You can choose two approaches: Domain Authentication and Single Sender Verification.

SendGrid Sender Authentication Setup

If your emails are not reaching users properly, authenticating the senders may improve the situation.


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Testing Django App UI on Mobile Devices

Check Developing App UI on Mobile Device

Customizing Django Allauth Templates

Django Allauth (7) – Allauth Template File Setup

Testing Django App UI on Mobile Devices

Check Developing App UI on Mobile Device

Customizing Django Allauth Templates

Django Allauth (7) – Allauth Template File Setup

Tags:

requirements.txt

Email Verification

Production Environment

SendGrid

.env

Amazon SES

API key

App Development

Mailtrap

Django Introduction
Course Content

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment

Chapter 1. Django Key Concepts

Web Framework and Django

Websites vs. Django Web Apps

How Django Handles HTTP Request and HTTP Response

Django's MVT Framework

Django Templates vs. Django APIs

Chapter 2. Django Quick Start Guide

Install Python

Install Visual Studio Code

Create Project Directory

Set Up Virtual Environment

Install Django

Start Django Project

Run Server

Database Migration

URL dispatcher – urls.py

Create Superuser and Log In to Django Admin

Start App

Create HTML Templates

Create Views

Add URL Patterns

Project vs. App

Chapter 3. Django Models and Databases

Create a Database in Django

Relational Database

Create Django Models

Makemigrations and Migrate

Add Models in Django Admin – admin.py

Change Display Name of Record Objects

Django Models – Data Field Type

Django Models – Field Options

Django Models – Help Text Option

Django Models – Choices Option

Django Models – DateField with datetime Module

Django Models – Relationship Fields

Django Models – ID

Django Models – ForeignKey (OneToMany Relationship)

Django Models – OneToOneField

Django Models – ManyToManyField

Chapter 4. Create CRUD Web Application

CRUD Web Application

Basic CRUD Structure in Django

Django Generic Views

How To Write Class-Based Views with Generic Views

Generic View Basic Attributes

URL Dispatcher for CRUD Views

Django Templates for CRUD Views

Django Template Language (DTL)

Template for List Page

get_FOO_display method

Template for Detail Page

Template with Model Relations

Template for Create and Update Page

Template for Delete Page

Add Links – {% url %} tag

Extend Templates – {% extends %} tag

Check Developing App UI on Mobile Device

Django Templates with Bootstrap

Crispy Forms

Customize Views (1) – Change List Order

Customizing Views (2) – Filter Lists

Context

Customize Views (3) – Add Extra Context

Modularize Templates – {% include %} tag

Static Files in Development Environment – {% static %} tag

STATIC_URL and STATICFILES_DIRS

Create Index HTML

Chapter 5. User Management

User Authentication

Overview of User Management Functions

User Management Function Development with Django

Approaches to Building User Management Functions in Django

Django Allauth (1) – Introduction

Django Allauth (2) – Installation and Initial Settings

Django Allauth (3) – Email Verification via Console

Django Allauth (4) – Email Verification via Gmail

Django Allauth (5) – Social Login with GitHub

Django Allauth (6) – Social Login with Google

Django Allauth (7) – Allauth Template File Setup

Django Allauth (8) – Add Basic Styling with Bootstrap and Crispy Forms

Django Allauth (9) – Customize Sign-in and Sign-up Pages

User Models

Login Required – LoginRequiredMixin

User Login Status Icon on Navigation Bar

Chapter 6. Deploy Django App

Overview of Django App Deployment (1)

Overview of Django App Deployment (2)

Key Steps of Django App Deployment

Hosting Service Initial Settings (1) – AWS Lightsail setup

Hosting Service Initial Settings (2) – SSH Remote Connection

Manage Local Computer and Remote Server Simultaneously

Tips for Managing Local Development and Remote Production Environment

Hosting Service Initial Settings (3) – Clone Project Directory with GitHub

Production Database Setup

Django Production Settings (1) – Settings.py for Development and Production

Django Production Settings (2) – Production Settings

Django Production Settings (3) – django-environ and .env file

Static File Settings

Django and Dependency Installation on Production Server

Web Server and Application Server in Django

Application Server Setup – Gunicorn

Web Server Setup – Nginx

Domain Setup

SSL Setup – Certbot

Email Setting – SendGrid

Social Login for Production

Manage Local Development and Remote Production Environment