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 a blog, implementing a mail delivery system is often necessary. However, building such a system is not easy, and many difficulties are involved in creating one on your own.

I once built and operated a service that combined Django and Gmail to deliver emails. However, even though I believed the settings were done correctly, emails were not delivered as expected on many occasions. This is where SendGrid comes in. 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 is very easy to integrate with your existing web application. It is currently one of the most popular email delivery systems 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 may have some additional restrictions beyond the difference in the number of email distributions, I have found the free plan to be sufficient for managing my website without any significant issues.

Two Ways to Integrate SendGrid into Django

There are two ways to integrate SendGrid into Django: using an SMTP server or the Web API. Most tutorials on using Django+SendGrid found online demonstrate the SMTP solution. However, the Web API is also a viable option.

In this document, I will describe how to use the Web API. For reference, a simple explanation of the SMTP solution can be found in the official documentation.

Environment for APP development

Integrate SendGrid on top of Django's basic setup. If you are using Django for the setup, please refer to this article.

Application Overview

We will create an application that generates a simple form and sends an email to the site owner when the form is submitted with valid input.

To confirm the implementation of SendGrid, we will only create the Form, Template, and corresponding View for this simple application. We will not create a Model.

We will use the following package to forward mail via web API.

https://github.com/sklarsa/django-sendgrid-v5

Project file structure

Implementation Process

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

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. Happy coding!