Automate Emails Using Python! Build An Automatic Payment Reminder & Schedule Your Scripts Online
Introduction
Are you still manually sending emails to your customers and clients? In this guide, I will show you how to send and schedule emails using Python, specifically by building a Payment Reminder tool. By the end, you’ll have a system that automatically sends reminders based on invoice due dates pulled from a Google Sheets document.
Project Preparation
Before jumping into the code, the first step is to set up the required dependencies. You will need to install two packages: pandas and python-dotenv. Open your terminal or command prompt and run:
pip install pandas python-dotenv
Next, I will handle the setup for our email credentials. Since we will be sending emails, we need to authenticate using our email address and password. Instead of hardcoding these sensitive details in the script, it’s best to store them in environment variables. To do this, create a .env file and add your email and password there. Make sure to also create a .gitignore file to prevent this sensitive information from being uploaded to GitHub.
Sending Emails
Now, let’s create a new Python file named send_email.py where we will write the logic to send emails. First, I will import the necessary modules: os, smtplib, email, and pathlib. After that, I will specify the port and email server for Outlook. If you use a different provider like Gmail, you will need to adjust the server and port settings accordingly.
Next, I will load our environment variables from the .env file and create a function to send emails. This function will take parameters like subject, receiver email, name, due date, invoice number, and amount. I will use the EmailMessage class to structure the email, including both plain text and HTML formats for better presentation.
Accessing & Querying Google Sheets Data
Now that we can send emails, let’s integrate our Google Sheets data. I’ve prepared a Google Sheets document that tracks invoices, containing fields for email, name, invoice number, amount, due date, reminder date, and payment status. To access this data, I will create a new Python file called main.py and use pandas to read the spreadsheet.
To convert the Google Sheets document to a CSV file, I will define the sheet ID and name in the script. Using pd.read_csv, I will load the data into a DataFrame and parse the due and reminder dates as DateTime objects for easy comparisons.
Testing the Local Python Solution
With the data loaded, I will create a function that iterates through the DataFrame and checks if the reminder date matches today’s date and whether the invoice has been paid. If both conditions are met, the function will call the send_email function to send out the reminder.
Deployment to the Cloud
Now that our local solution is working, it’s time to deploy it to the cloud. I recommend using Deta, which offers free cloud services. First, create a requirements.txt file to specify the external libraries needed for the project, which will include pandas and python-dotenv.
Follow the steps to set up your Deta account, install the Deta CLI, and log in. Once logged in, create a new micro using the command line, and ensure your main.py file is in the correct directory.
Testing the Cloud Solution
Before deploying, set your environment variables in Deta using the deta update -e command. Then, set up a cron job to schedule your script. You can define how often you want your script to run, such as every minute for testing purposes.
After deploying, you can check the Deta visor to see if your script runs successfully. It’s important to validate that your script is sending emails as expected by checking your inbox.
Outro
In this guide, I demonstrated how to automate email reminders using Python and Google Sheets. You can now schedule your scripts online for free, ensuring that your customers receive timely payment reminders without manual intervention. Adjust the reminder dates in your spreadsheet, and your automated system will handle the rest.