In this blog, we will demonstrate how to build an email scheduler using the QStash Python SDK in combination with SendGrid and Django.
Here is a live demo of the project deployed on Vercel for you to try it out.
Motivation
Being able to schedule emails is quite important for many applications. Whether you are sending reminders, newsletters, or notifications, automating your emails ensures your messages are always delivered on time which can save you lots of time. Using QStash, it has never been easier to schedule messages to be sent at a later time. After reading this post all your emails will be delivered on time, every time.
Prerequisites
To follow along with this tutorial, you will need:
Basic knowledge of Python and Django.
A SendGrid account and API key for sending emails.
An Upstash account to get your QStash token.
Project Setup
Install Necessary Packages
Install QStash Python SDK, Django, SendGrid and other necessary packages:
QStash Python SDK is used to interact with QStash, SendGrid is used to send emails, django is used to create the web application, croniter is used to validate CRON expressions, and python-dotenv is used to load environment variables from a .env file.
Create a Django Project
First, set up a new Django project. Navigate to your desired directory and run:
Configure Django Settings
Add scheduler to your INSTALLED_APPS and set APPEND_SLASH to False in the project's settings.py:
Add your SendGrid and QStash configurations to your .env file:
Implementing the Email Scheduler
Getting Environment Variables
In scheduler/utils/helpers.py, create a helper function to get environment variables:
Send Emails Using SendGrid
In scheduler/utils/send_email.py, create a function to send emails using SendGrid:
Schedule Emails Using QStash
Create a function to schedule emails using QStash in scheduler/utils/email_scheduler.py:
And another function to the same file to schedule emails using CRON expressions:
Create a Django View to Handle Scheduling
In scheduler/views.py, add a view to handle email scheduling requests:
We use the @csrf_exempt decorator to allow POST requests without CSRF tokens. This view handles both specific date and time scheduling and CRON string scheduling. The schedule_email function schedules emails to be sent after a specific delay, while the schedule_email_cronjob function schedules emails based on a CRON expression.
Create a Django View to Handle Email Sending
In scheduler/views.py, add a view to handle email sending requests:
The URL of this view will be used as the destination URL in the QStash message once it is deployed. The view receives the email data as a JSON object and sends the email using the send_email function.
Create URL Patterns
In scheduler/urls.py, add URL patterns for the views:
Update the Project's URL Patterns
We will also add the URL patterns for the scheduler app to the project's URL patterns in email_scheduler/urls.py:
Create a Template for Scheduling Emails
Create a template scheduler/templates/schedule_email.html:
You can also add some CSS to style the template:
And with that, the project is complete!
Conclusion
In this tutorial, we have shown how to build an email scheduler using the QStash Python SDK, SendGrid, and Django. This project helps you automate your emails, ensuring you communicate with your users consistently and on time.
For more detailed information, explore the Upstash QStash documentation. You can find the complete source code for this project on the GitHub repository. For any questions or feedback, feel free to reach out to me on LinkedIn.