Sending beautiful letters is an important part of any modern application. Whether it's registration, notification, or mailing, a simple text/plain no longer evokes emotions. But HTML letters are real marketing 💥
In this article, we will analyze how to create and use email templates with HTML and CSS inside a Python project. It's simple and very useful!

🔧 Why do you need HTML templates for emails?
📌 Organize the structure of the letter
🖼 Add styles, buttons, images
👀 Make the letter visually appealing
📱 Adapt for mobile devices
🛠️ What are we going to use?
smtplib— for sending emailsemail.message.EmailMessage— for generating a letterJinja2— for generating HTML with variablesHTML + встроенный CSS— for registration
📁 Project structure
email_project/
├── main.py
├── templates/
│ └── welcome.html🧠 Step 1: HTML letter template (templates/welcome.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body { font-family: Arial, sans-serif; background-color: #f9f9f9; padding: 20px; }
.container { background: white; padding: 30px; border-radius: 10px; max-width: 600px; margin: auto; }
h1 { color: #333; }
p { color: #555; }
.button { background-color: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; }
</style>
</head>
<body>
<div class="container">
<h1>Привет, {{ name }}!</h1>
<p>Спасибо за регистрацию. Добро пожаловать в наш проект!</p>
<a href="{{ link }}" class="button">Подтвердить email</a>
</div>
</body>
</html>🧪 Step 2: Generate a letter in main.py
import smtplib
from email.message import EmailMessage
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template('welcome.html')
html_content = template.render(name="Code", link="https://itcodik.com/confirm")
msg = EmailMessage()
msg['Subject'] = 'Welcome!'
msg['From'] = 'noreply@example.com'
msg['To'] = 'user@example.com'
msg.set_content("You have a new email")
msg.add_alternative(html_content, subtype='html')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login('your_email@gmail.com', 'your_password')
smtp.send_message(msg)Important: never hardcode a password — use environment variables or .env
📌 Tips for letter layout
What to consider | Why it matters |
|---|---|
✅ Inline styles | Mailers cut <style> |
✅ Simple structure | Better <table> for reliability |
❌ Do not use JS | It does not work in emails |
✅ Images via URL | Do not attach as files unnecessarily |
✅ Test in Gmail, Outlook | Different display engines |
📤 Examples of use
Registration: confirmation letter
Password recovery
Newsletter
Order notification
Letter of support
👀 How to test the letter?
You can save the HTML file locally:
with open("preview.html", "w", encoding="utf-8") as f:
f.write(html_content)🧩 Where it comes in handy
In Flask projects (
flask-mail)In Django (
EmailMultiAlternatives)In FastAPI via asynchronous SMTP libraries
In server bots, notifications, CRM integrations
🧠 Conclusion
Creating HTML emails in Python is easy! The main thing is a template, a little style, and a neat submission.
If you want to experiment with mailings and autogeneration of letters — in the application Code you will find step-by-step courses, templates, and community support. Come in or write to our Telegram chat ✌️
💬 Do you want a letter template with adaptive layout or integration with Mailgun / SendGrid? Write to us — we'll make a sequel!
