Skip to content

Use a template for styling password reset emails #25

@chriscarrollsmith

Description

@chriscarrollsmith

Create the following test.py file at the project root and run it to live-test the email templating and sending functionality:

# Replace "Valid Name" and "[email protected]" with a real name and email
# change the send_email boolean value to control whether to send or just render the template

from jinja2 import Template
from fastapi.templating import Jinja2Templates
from sqlmodel import Session
from utils.db import engine, set_up_db
from utils.auth import send_reset_email
from utils.models import User

send_email = False

if send_email:
    with Session(engine) as session:
        set_up_db(drop=True)
        user = User(
            name="Valid Name",
            email="[email protected]"
        )
        session.add(user)
        session.commit()
        send_reset_email(user.email, session)
else:
    templates = Jinja2Templates(directory="templates")

    # Render the email template
    template: Template = templates.get_template(
        "emails/reset_email.html"
    )
    html_content: str = template.render({"reset_url": "https://example.com"})

    # Open a file to write the rendered template to
    with open("rendered_email.html", "w") as f:
        f.write(html_content)

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions