Configuring a email sender with FastAPI

{
    "1-click": {
        "template": "1_click.html",
        "recipients": { "to": ["[email protected]"], "cc": ["[email protected]"] },
        "subject": "Welcome to 1-Click",
        "required_variables": ["username", "welcome_message"]
    },
    "2-click": {
        "template": "2_click.html",
        "recipients": { "to": ["[email protected]"], "cc": ["[email protected]"] },
        "subject": "Welcome to 2-Click",
        "required_variables": ["username", "confirmation_link"]
    }
}

def send_email(email_type: str, variables: dict):
    config = EMAIL_CONFIG.get(email_type)
    if not config:
        raise ValueError(f"Invalid email type: {email_type}")


    required_vars = config.get("required_variables", [])
    missing_vars = [var for var in required_vars if var not in variables]
    if missing_vars:
        raise ValueError(f"Missing required variables for '{email_type}': {', '.join(missing_vars)}")
    

    template = env.get_template(config["template"])
    email_body = template.render(**variables)
    recipients = config["recipients"]
    


I want to send various emails to various users, based on the name of the email API that I need to use.

1-click sends to 1 click users and etc.

Can someone help me understand if this is the best way to go?

the number of emails to be sent is less than 5000.

How do I handle the variables that need to be sent as part of the template? each email has its own variables too?

I am planning on using FastAPI background tasks for this. Is there any way i can do it better?

Upvotes: 0

Views: 44

Answers (0)

Related Questions