Level Up Your Email Game: How to Send Personalized HTML Emails with Python

Ahmed Yasin
3 min readOct 17, 2024

In a world where digital communication reigns supreme, standing out in your audience’s inbox can feel like an uphill battle. Whether you’re reaching out to clients, partners, or potential investors, personalized communication is crucial. Today, I’m excited to share a simple yet powerful Python script that automates the process of sending customized HTML emails. And trust me, it’s not just for investment proposals — it can be a game-changer for newsletters, event invitations, and much more.

Why Bother with Email Automation?

Let’s face it: manual email outreach can be a tedious and time-consuming task. But when you automate your outreach, you not only save precious time but also ensure your messages remain consistent and error-free. Personalization matters — people appreciate hearing their names and receiving tailored content that speaks directly to them. This script takes your outreach to the next level, allowing you to send dozens or even hundreds of emails in just a few minutes. Imagine the possibilities!

What This Script Does

Our email-sending script uses Python’s smtplib and email libraries to create and send visually appealing HTML emails. Here’s a snapshot of what it can do:

  • Personalization: Each email can be customized with the recipient’s name and other unique details.
  • HTML Formatting: Spice up your emails with colorful visuals and proper formatting to grab attention.
  • CSV Integration: Easily manage your recipient list by reading from a CSV file.

Let’s Dive into the Code

Here’s a step-by-step breakdown of how the script works:

  1. Get the Essentials: First, we need to import the libraries that will help us send emails and handle our recipient data.
import smtplib 
from email.mime.multipart
import MIMEMultipart
from email.mime.text
import MIMEText import csv
  1. Configure Your SMTP Server: Next, you’ll set up your SMTP server details, including your server address, port, user account, and password. This is where the magic happens!
smtp_server = 'your.smtp.server' 
smtp_port = 587 # or another port based on your SMTP server
smtp_user = 'your_email@example.com'
smtp_password = 'your_password'
  1. Craft Your Email Content: Here, you’ll define the subject line and the HTML body of your email. Feel free to get creative!
subject = 'Your Subject Here' 
html_body = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Title</title>
</head>
<body>
<p>Hi {name},</p>
<p>Your content goes here.</p>
<p>Best regards,<br>Your Name</p>
</body>
</html>
'''
  1. Load Your Recipient List: The script reads recipient data (like names and email addresses) from a CSV file. This makes it super easy to manage your outreach list.
with open('recipients.csv', newline='') as csvfile:     
reader = csv.DictReader(csvfile)
for row in reader:
name = row['firstname']
to_email = row['emails']
...
  1. Send the Emails: For each recipient, we personalize the email and send it off using the configured SMTP server.
msg = MIMEMultipart('alternative') 
msg['From'] = smtp_user
msg['To'] = to_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'html'))
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls() # Use TLS for security
server.login(smtp_user, smtp_password)
server.sendmail(msg['From'], to_email, msg.as_string())
  1. Track Your Success: Finally, the script keeps track of how many emails were successfully sent and how many failed, giving you a clear summary at the end.
print(f"Total successful emails: {successful_sends}") 
print(f"Total failed emails: {failed_sends}"

Beyond Investment Proposals

While this script is perfect for investment outreach, its potential doesn’t stop there. Here are some other ways you can use it:

  • Newsletters: Keep your subscribers informed and engaged with beautifully crafted newsletters.
  • Event Invitations: Make your invitations feel personal and special.
  • Promotional Campaigns: Reach out to potential customers with tailored offers and promotions.
  • Customer Follow-Ups: Thank customers for their business and offer them additional products or services.

Here you can find Github link for full code.
GITHUB : https://github.com/ahmedyasin21/CustomEmailSender

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ahmed Yasin
Ahmed Yasin

Written by Ahmed Yasin

Youtuber / Content Writer / C++ /Learning Java/ https://www.youtube.com/channel/UC-gDcN99rYbeyH1oKsoPM3A Instagram : belongs_to_mars

No responses yet

Write a response