Harness the Power of Python To Send Emails Programmatically
A step-by-step tutorial on sending emails using Python
Programming can be used for a lot of things. From creating simple math functions to building mobile apps. It can also be used to automate specific tasks to be done in seconds, as opposed to doing it yourself which could take hours. As we have developed unique and special ways to utilize code, more and more tasks have become automated. And one of these tasks is — sending emails.
But isn’t sending emails already an easier way of delivering information? Emails did essentially replace physical mail however it still requires some form of manual intervention. These forms are (but are not limited to):
- Typing up the email
- Scheduling or knowing when to send the email
- Determining who is receiving the email
I’m sure there are more, but these are the most obvious ones. Anyways, what if we could automate sending an email with code? Specifically with Python? The applications of such a program can range from scheduled email reminders to mass marketing emails for a company. As you read on, you will learn how to quickly set up a simple Python script to send an email without the need of using an email provider like Gmail.
Choose an Email Service
Not talking about Gmail or yahoo, but an email service with an API. Using such a service will allow you a lot of flexibility to send multiple emails with an easy-to-use API. I’ve found that this is one of the easiest ways to get started with sending an email in Python.
The email service that I use is called SendGrid. It is free to use and easy to set up an account. The free tier plan for the service allows you to send up to 100 emails every day. It’s also developer-friendly with documentation that supports Python. We will be using SendGrid for the rest of this article tutorial.
Getting Started
In order to get started with SendGrid, you’ll need to create a free account. After you’ve done that, follow these steps:
- Verify your sender email address — In the dashboard sidebar, go to Settings to Sender Authentication to Single Sender Verification to Create New Sender. Enter the required info, and go to the email inbox to verify the address.
- Get the API Key — Go back to Settings to API Key to Create API Key. Get full access and name it. Click Create & View to view and copy the API key for future usage.
And that’s it. If you want to add more email addresses to send from, you’ll need to verify them like before. With the API key and verified email address, you’re ready to get started with the coding.
1. Install SendGrid
Assuming you are using Jupyter notebooks or Jupyter lab, you can install SendGrid directly from Jupyter with the following command:
!pip install sendgridOtherwise, you can just do it in your command terminal with the same command above but remove the exclamation mark — pip install sendgrid.
2. Import SendGrid Libraries and Set the API Key
After you’ve installed SendGrid, you can import the libraries within the package:
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import MailNext, you’ll want to use the API key and assign it to the SendGrid client. You’ll need to keep your API key private. I kept mine in a text file outside of the code to access it for assignment when needed:
# Importing and assigning the api key
with open("../sendGrid-API.txt", "r") as f:
api_key = f.read()
# Using the api key with the sendgrid client
sg = SendGridAPIClient(api_key)3. Set the Email Variables
Here you’ll need to set the different variables for your email. The variable names are pretty self-explanatory:
sender = "[email protected]"recipient = "[email protected]"subject = "Hey Check Out This Test Email!"content = "I just sent this email with Python. So cool!"4. Construct the Email Object
This is where we’ll create the email object from SendGrid. Just use the variables above and assign them to the appropriate parameters:
email = Mail(
from_email=sender,
to_emails=recipient,
subject=subject,
html_content=content
)5. Send the Email
Here we will actually send the email object we constructed above by using the SendGrid client:
response = sg.send(email)# Printing out the response
print(response.status_code)Print out the response status code in order to know if the email has been accepted and is ready to ship out.
Additionally, check the recipient’s inbox if you can to see if you’ve received the email.
6. Putting It All Together
Now in order to make the previous code flow collectively, we’ll combine all the elements above into a single function. With the variables being the only thing we need to adjust. Here’s some code:


