Automating Birthday Emails with Python

Rupam Biswas
4 min readSep 18, 2024

--

In the era of technology, automation has become an essential skill, allowing us to streamline repetitive tasks and increase productivity. One area where automation can truly shine is in email management. As a Python learner eager to enhance my skills, I recently embarked on a project to automate birthday emails. This project not only provided me with hands-on experience but also reinforced the practical applications of Python in everyday tasks.

Python

The Inspiration

The idea for this project stemmed from a common challenge: remembering birthdays. With friends and family spread out across different time zones, it’s easy to forget special dates. Automating birthday greetings can ensure that no one is left out, making it a thoughtful gesture that requires minimal effort on my part.

Project Overview

The goal of my project was simple: create a script that sends automated birthday emails to individuals whose birthdays fall on the current day. To achieve this, I used Python along with a few libraries, including pandas for data manipulation and smtplib for sending emails.

Tools and Libraries Used

  1. Python: The primary programming language for the project.
  2. pandas: A powerful data manipulation library that allows for easy reading and processing of CSV files.
  3. smtplib: A built-in Python library for sending emails via the Simple Mail Transfer Protocol (SMTP).
  4. Random: To select a random letter template for personalized emails.

Step-by-Step Implementation

1. Setting Up the Environment

First, I ensured that Python was installed on my machine and set up a virtual environment for the project. I also installed the pandas library, which is not included in the standard library.

pip install pandas

2. Creating the CSV File

I created a CSV file named birthdays.csv to store the names, birth dates, and email addresses of individuals.

3. Crafting Letter Templates

To make the emails more personalized, I created a directory named letter_templates containing text files with birthday messages. The files were named letter_1.txt, letter_2.txt, and letter_3.txt, each containing a different message format. Each message included a placeholder for the name.

4. Writing the Python Script

The core of the project was a Python script that checks the current date against the birthdays in the CSV file and sends an email if there is a match. Here’s the complete code:

from datetime import datetime
import pandas as pd
import random
import smtplib

# Your email and password
MY_EMAIL = "YOUR EMAIL"
MY_PASSWORD = "YOUR PASSWORD"

# Get today's date
today = datetime.now()
today_tuple = (today.month, today.day)

# Load birthdays from the CSV file
data = pd.read_csv("birthdays.csv")
birthdays_dict = {(data_row["month"], data_row["day"]): data_row for (index, data_row) in data.iterrows()}

# Check if today is someone's birthday
if today_tuple in birthdays_dict:
birthday_person = birthdays_dict[today_tuple]

# Select a random letter template
file_path = f"letter_templates/letter_{random.randint(1, 3)}.txt"
with open(file_path) as letter_file:
contents = letter_file.read()
contents = contents.replace("[NAME]", birthday_person["name"])

# Set up the SMTP connection and send the email
with smtplib.SMTP("YOUR EMAIL PROVIDER SMTP SERVER ADDRESS") as connection:
connection.starttls() # Upgrade to a secure connection
connection.login(MY_EMAIL, MY_PASSWORD) # Log in to your email account
connection.sendmail(
from_addr=MY_EMAIL,
to_addrs=birthday_person["email"],
msg=f"Subject: Happy Birthday!\n\n{contents}"
)

5. Testing the Automation

After writing the script, I ran several tests to ensure everything worked smoothly. I set different dates in the CSV file and verified that the correct email was sent. I also checked that the letter templates were correctly formatted and that the names were replaced appropriately.

6. Security Considerations

To ensure security, I handled my email credentials carefully. Instead of hardcoding them directly in the script, I used environment variables or a configuration file to store sensitive information.

Key Takeaways

This project provided me with valuable insights into the world of automation and practical applications of Python:

  1. Understanding Email Protocols: I gained a foundational understanding of how SMTP works and how to send emails programmatically.
  2. Data Handling with pandas: I learned how to read and manipulate data from CSV files, a crucial skill for many data-driven projects.
  3. The Importance of Personalization: By using letter templates, I understood the impact of personalization in communication, even in automated processes.

Next Steps

Looking ahead, I’m excited to explore further enhancements for this project:

  • Using OAuth2: Implementing OAuth2 for secure authentication instead of using plain email passwords.
  • Scheduling Emails: Using Python’s schedule library to run the script at a specified time each day automatically.
  • Adding More Features: Including features like sending reminders a few days before someone’s birthday.

Conclusion

Automating birthday emails using Python has been a rewarding experience that deepened my understanding of automation, data handling, and email protocols. I encourage anyone looking to improve their programming skills to embark on similar projects, as they provide practical experience and valuable learning opportunities.

If you’re interested in the full code, check out my GitHub repository:

GitHub Link

Feel free to share your thoughts or experiences with automation in the comments below. Happy coding!

--

--