Are you sending emails from within a web app? If yes, you need to do so asynchronously or else you'll block requests and prevent completion. Without the right solution, you'll end up with additional, slow requests in order to construct the content of your emails, reducing productivity and impacting your efficiency.
If you are seeking a solution for queueing emails, Python libraries offer many answers, including celery. If you're using Python 3.4 or above, you could also use asyncio. However, Iron is generally the friendliest solution to try. Here's how to use Python and Iron together for queueing emails.
Want to try this solution for yourself? Book a free demo of Iron to get started.
Why Use This Integration?
SendGrid is a popular SMTP provider, best known for helping businesses manage their marketing and transactional emails without the need to maintain your own email server. Instead, you're able to send emails using the cloud with SendGrid's API or SMTP relay. Using SendGrid can save you thousands of dollars worth of investments in expensive email server infrastructure, but SendGrid alone isn't perfect.
If you've been using SendGrid for some time, you might run into error messages ranging from "temporary local problems" to challenges queuing or delivering mail. These problems aren't just frustrating, but they also slow down your delivery and can lead to recipients not getting important emails. That's why you should consider integrating with Iron.io.
IronWorker's container-based platform takes the coding and dependencies out of SendGrid tasks, allowing you to process them on-demand. By using Docker containers, IronWorker creates a multi-language environment that's easy to work with and even easier to scale. In this guide, we'll take you through the simple steps that will allow you to integrate SendGrid with Iron using Python.
Getting Started With Queueing Emails
In order to follow along with this tutorial, you will need:
- Python 3.5 or later
- A free SendGrid account
- An email address that you can use for testing
Start by creating a new Python project that you can use for testing. Create a file titled build_mail.py and open it in your favourite editor.
Integrate Iron and SendGrid
Create a worker for SendGrid using IronWorker so you can schedule emails with ease. IronWorker utilizes the Node.js runtime, which is compatible with SendGrid's Node.js library. Use the command-line interface within IronWorker to deploy your worker to IronWorker's cloud environment and then use your worker with Iron's API to process, schedule, or repeat SendGrid jobs with ease.
Don't have Iron set up yet? Take the first step and book a free demo.
Iron.io Serverless Tools
Speak to us to learn how IronWorker and IronMQ are essential products for your application to become cloud elastic.
Create the Email Queue
Go to your SendGrid dashboard and create an API key. For this example, we'll call it "Bulk Email." Click "Create & View" so you can see your key. Save it as an environment variable called SENDGRID_API_KEY before you close the dialogue box because you will not be able to view it again for security purposes.
Install the SendGrid Python helper library using the command:
pip install sendgrid
SendGrid is now set up for the integration.
Generate the Email Content
The next step is to write a function to send your email using the SendGrid API.
import os from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail def send_message(sendgrid_client, email): pass message = Mail( from_email=('email@example.com', 'IronMQ Integration'), to_emails=email, html_content=get_html_content(), subject='Let's Integrate!', ) try: response = sendgrid_client.send(message) print(response.status_code) print(response.body) print(response.headers) except Exception as e: print(e) sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) email = ['firstname.lastname@example.org'] # your email goes here send_message(sendgrid_client, email)
You only need to do the above once with the SendGrid client. However, you need to enqueue a separate job for every queued email you want to send out, which is why you must pass the sendgrid_client and email parameters to the send_message function.
Using the command line, run this script again by using this code:
202 b'' Server: nginx Date: Wed, 24 Dec 2021 22:14:23 GMT Content-Length: 0 Connection: close X-Message-Id: eKeK_Wgf9FR1FSyXb8WBve Access-Control-Allow-Origin: https://sendgrid.api-docs.io Access-Control-Allow-Methods: POST Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl Access-Control-Max-Age: 600 X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html
If your mail server is set up correctly, you should now receive an email with the subject "Let's Integrate!" (or whatever you entered into your code) at the address you specified.
Get More Out of Your Message Queue
Now that you know how to use Python, SendGrid, and Iron together, why not see what else you can do with Iron apart from queueing emails? Explore Iron's full suite of solutions and, if you haven't yet, book your free demo now to see what Iron can do for your business.
About Korak Bhaduri
Korak Bhaduri, Director of Operations at Iron.io, has been on a continuous journey exploring the nuances of serverless solutions. With varied experiences from startups to research and a foundation in management and engineering, Korak brings a thoughtful and balanced perspective to the Iron.io blog.
This site uses Akismet to reduce spam. Learn how your comment data is processed.