Email Templates with Jinja in Python
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 = "sender@email.com" 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" : "john@example.com" }, { "name" : "Jane Smith" , "email" : "jane@example.com" }, { "name" : "Bob Johnson" , "email" : "bob@example.com" }, ] # 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