Email Templates with Jinja in Python
Last Updated :
28 Apr, 2025
HTML emails play a crucial role in modern communication, whether for newsletters, notifications, or transactional emails. Jinja, a powerful templating engine for Python, offers flexibility for creating dynamic and visually appealing HTML email templates. In this article, we will see how we can use Jinja for HTML Email Templates in Python.
Jinja for HTML Email Templates in Python
Below are the steps by which we can use Jinja for HTML Email Templates in Python:
Step 1: Setting Up Jinja2
Before we start doing anything with Jinja we'll need to have it on our system. Use the command below to install it. Ignore if you already have Jinja2 installed.
pip install Jinja2
This command will install the Jinja2 templating engine on your system and now we are ready to proceed further.
Step 2: Creating a Basic Email Template (template.html)
Let's start with a simple email template named template.html. This is an HTML template that we will use with our Jinja2 templating engine, the placeholders in double curly braces {{ }} will be filled in by Jinja. This template also has some styling to show that we can use CSS and remote images.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
body {
background: url("https://unsplash.com/photos/\
7dlm5aRa4Qw/download?ixid=M3wxMjA3fDB8MXxhbGx8NXx8f\
Hx8fDJ8fDE2OTk4ODA0Mzd8&force=true&w=1920")
no-repeat center center fixed;
}
.container {
text-shadow: 10px 10px 10px #000000;
background: linear-gradient(to right, #00000088, #ffffff1d);
color: rgb(198, 198, 198);
padding-bottom: 10px;
padding-left: 5px;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>{{ subject }}</h1>
</header>
<main>
<p>{{ greeting }}</p>
<p>{{ message }}</p>
<p>Best regards, <br>{{ sender_name }}</p>
</main>
<footer>
This email was sent from <i>GFG</i>.
</footer>
</div>
</body>
</html>
Step 3: Jinja2 in Python (render.py)
Now, let's create a Python script to render this template using Jinja2 and send it to the respective recipients. This Python script utilizes Jinja2 to create personalized emails by merging data from a template with recipient details. It establishes a connection to an SMTP server, iterates over a list of recipients, generates customized emails using Jinja2, and sends them using the SMTP server. The email content includes a greeting, a predefined message, and the sender's name.
Python3
# First let's get jinja2
from jinja2 import Template
# We will need smtplib to connect to our smtp email server
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Read the Jinja2 email template
with open("template.html", "r") as file:
template_str = file.read()
jinja_template = Template(template_str)
# Define email server and credentials
smtp_server = "smtp.server.com"
smtp_port = 587
sender_email = "[email protected]"
sender_password = "password"
# Set up email server
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender_email, sender_password)
# Define recipients and their details
# This can be read from some CSV file or passed from some other program or API
people_data = [
{"name": "John Doe", "email": "[email protected]"},
{"name": "Jane Smith", "email": "[email protected]"},
{"name": "Bob Johnson", "email": "[email protected]"},
]
# Now we iterate over our data to generate and send custom emails to each
for person in people_data:
# Create email content using Jinja2 template
email_data = {
"subject": "Greetings from Jinja Email",
"greeting": f"Hello {person['name']}!",
"message": "This is a sample email generated using Jinja2.",
"sender_name": "GFG",
}
email_content = jinja_template.render(email_data)
# Create the email message
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = person["email"]
msg["Subject"] = email_data["subject"]
# Attach the HTML content to the email
msg.attach(MIMEText(email_content, "html"))
# Print and send the email
print(f"Sending email to {person['email']}:\n{email_content}\n\n")
server.sendmail(sender_email, person["email"], msg.as_string())
# Close the server connection
server.quit()
Add your own SMTP server, and account details and fill in your recipient details. You will get this as an email in your recipient account if you used the template in this article
Output

Similar Reads
Python Falcon - Jinja2 Template Python Falcon is a lightweight and minimalist web framework designed for building web APIs, with a particular emphasis on simplicity, speed, and efficiency. Falcon is developed to handle HTTP requests efficiently and is optimized for performance, making it a suitable choice for developers who priori
6 min read
Getting started with Jinja Template This article introduces Jinja and then describes the basics of working with it which includes how to render templates using Jinja and Python3, passing data to the template through arguments, and basic Jinja syntax (delimiters, expressions, control structures - decisions and loops). A few examples in
8 min read
Templating With Jinja2 in Flask Flask is a lightweight WSGI framework that is built on Python programming. WSGI simply means Web Server Gateway Interface. Flask is widely used as a backend to develop a fully-fledged Website. And to make a sure website, templating is very important. Flask is supported by inbuilt template support na
6 min read
Placeholders in jinja2 Template - Python Web pages use HTML for the things that users see or interact with. But how do we show things from an external source or a controlling programming language like Python? To achieve this templating engine like Jinja2 is used. Jinja2 is a templating engine in which placeholders in the template allow wri
5 min read
Switching From Other Template in Python Jinja Jinja, a template engine, seamlessly combines the power of Python with HTML's structure. It enhances the development experience, making web applications more flexible. Jinja solves the challenge of cleanly integrating server-side logic (Python) with client-side presentation (HTML). It eliminates clu
4 min read