rx-36

Creating a contact form using SendGrid with Django

This article will guide you through building a simple contact form using Django and SendGrid.

When running a web service or blog, implementing an email delivery system is often necessary. However, building such a system from scratch is not easy and involves many challenges.

I once built and operated a service that combined Django and Gmail to deliver emails. However, even though I believed the settings were correct, emails often failed to deliver as expected.

When I faced this problem, SendGrid became my solution.

As a cloud-based email delivery system, it is easy to set up, and in my experience, email delivery problems rarely occur when using SendGrid.

Furthermore, SendGrid integrates easily with your existing web application. It is one of the most popular email delivery services among web developers.

In this article, I will show you how to use SendGrid in Django to easily send emails through a contact form.

About SendGrid's Free Tier

SendGrid offers both free and paid plans. The free plan is limited to sending 100 emails per day.

While the free and paid plans have some additional differences beyond email volume, the free tier should be sufficient for testing purposes.

Two Ways to Integrate SendGrid into Django

There are two primary ways to integrate SendGrid with Django: using an SMTP server or leveraging the Web API. Most online tutorials covering Django and SendGrid tend to focus on the SMTP solution.

However, SMTP has some drawbacks that make it less suitable for modern web applications. It can be slower than the Web API and offers limited error messages, making debugging more difficult. Therefore, if you're integrating email functionality into your Django project, I recommend building your system around the Web API.

This article will demonstrate how to send emails using SendGrid's Web API through the django-sendgrid-v5 package, which integrates with Django's built-in email functions.

This approach is the easiest way to use SendGrid with Django while establishing a foundation for a scalable and robust email delivery system.

Environment for application development

After creating a basic Django application, we'll integrate SendGrid. The process of creating a basic Django application won't be covered here.

If you're using Docker, please refer to this article to set up a basic Django application within a Docker container.

Application Overview

We'll create an application that displays a simple form and sends an email to the site owner when valid input is submitted.

To keep things simple and focus on SendGrid integration, we'll only create a form, template, and corresponding view. We will not create a Model.

We'll use the following package to send email via the Web API.

django-sendgrid-v5

Project file structure

Implementation Process

The process might involve some back and forth, but generally, it follows these steps:

  1. Setup of SendGrid
  2. Install the package
  3. Setup of settings.py
  4. Set up urls.py in the Django root folder and create urls.py in the application folder
  5. Create a forms.py
  6. Create Templates
  7. Create a views.py

1. Setting up SendGrid

2. Installing the Package

3. settings.py

4. urls.py

5. forms.py

6. Templates

7. views.py

That's all! With SendGrid, you can create a reliable email delivery environment with a simple setup and code, as shown above. There's no reason not to use it, so let's all start using it actively.

I've also created a GitHub repository for this application, which you can access here.

Happy coding!