0
0
Flaskframework~5 mins

HTML email templates in Flask

Choose your learning style9 modes available
Introduction

HTML email templates let you send pretty, styled emails instead of plain text. They make your emails look nice and easy to read.

When sending welcome emails to new users with your brand colors and logo.
When sending order confirmations that show product details clearly.
When sending newsletters with images and links.
When you want your emails to look professional and trustworthy.
When you want to include buttons or styled text in emails.
Syntax
Flask
from flask import Flask, render_template
from flask_mail import Mail, Message

app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.example.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USERNAME'] = 'your-email@example.com'
app.config['MAIL_PASSWORD'] = 'your-password'
app.config['MAIL_USE_TLS'] = True

mail = Mail(app)

@app.route('/send-email')
def send_email():
    msg = Message('Hello from Flask', sender='your-email@example.com', recipients=['friend@example.com'])
    msg.html = render_template('email_template.html', name='Friend')
    mail.send(msg)
    return 'Email sent!'

# email_template.html is a separate HTML file with your email design.

Use render_template to load your HTML email file and fill in dynamic parts.

Set msg.html to the rendered HTML string to send styled emails.

Examples
This loads the HTML template and replaces {{ name }} with 'Alice'.
Flask
msg.html = render_template('email_template.html', name='Alice')
Example of a simple HTML email template using Jinja2 placeholders.
Flask
<html>
  <body>
    <h1>Hello {{ name }}!</h1>
    <p>Welcome to our site.</p>
  </body>
</html>
Sample Program

This Flask app sends a styled HTML email using a simple template string. It replaces {{ name }} with 'Friend'.

Flask
from flask import Flask, render_template_string
from flask_mail import Mail, Message

app = Flask(__name__)
app.config.update(
    MAIL_SERVER='smtp.example.com',
    MAIL_PORT=587,
    MAIL_USERNAME='your-email@example.com',
    MAIL_PASSWORD='your-password',
    MAIL_USE_TLS=True
)

mail = Mail(app)

email_html = '''
<html lang="en">
  <body style="font-family: Arial, sans-serif;">
    <h2 style="color: #2a9d8f;">Hello {{ name }}!</h2>
    <p>Thanks for joining our community.</p>
    <p>We hope you enjoy your stay.</p>
  </body>
</html>
'''

@app.route('/send-email')
def send_email():
    msg = Message('Welcome!', sender='your-email@example.com', recipients=['friend@example.com'])
    msg.html = render_template_string(email_html, name='Friend')
    mail.send(msg)
    return 'Email sent!'

if __name__ == '__main__':
    app.run(debug=True)
OutputSuccess
Important Notes

Always test your HTML email in different email clients to check how it looks.

Keep your HTML simple and inline styles for best compatibility.

Use Flask-Mail to handle sending emails easily in Flask apps.

Summary

HTML email templates make emails look nice and professional.

Use Flask's render_template or render_template_string to create email content.

Send emails with Flask-Mail by setting the html property of the message.