Chapter 6. Deploy Django App

Email Setting – SendGrid

Email Setting – SendGrid
Tag:

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.

Email-Setting--SendGrid

Type your email and password.

Email-Setting--SendGrid

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.

Email-Setting--SendGrid

Select Full Access unless you want to customize the setting.

Email-Setting--SendGrid

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

Email-Setting--SendGrid

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.

Email-Setting--SendGrid

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 confrimation message.

Email-Setting--SendGrid

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

Email-Setting--SendGrid

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-Setting--SendGrid

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

Email-Setting--SendGrid

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.

Email-Setting--SendGrid

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

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.

Email-Setting--SendGrid

Type your email and password.

Email-Setting--SendGrid

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.

Email-Setting--SendGrid

Select Full Access unless you want to customize the setting.

Email-Setting--SendGrid

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

Email-Setting--SendGrid

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.

Email-Setting--SendGrid

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 confrimation message.

Email-Setting--SendGrid

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

Email-Setting--SendGrid

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-Setting--SendGrid

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

Email-Setting--SendGrid

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.

Email-Setting--SendGrid

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

Tag: