Skip to content

Title: Improve flexibility of email template configuration (support for multiple template folders) #308

@DamirKFU

Description

@DamirKFU

Current implementation of MultiTemplateConnectionConfig is a bit rigid. Template folders are defined statically via get_all_templates() and TEMPLATE_FOLDER is not used.

It would be great to make this more flexible:

allow passing template folders dynamically (not only at class level)
either remove or properly use TEMPLATE_FOLDER
make it easier to extend/override templates per environment or use case

class MultiTemplateConnectionConfig(ConnectionConfig):
    TEMPLATE_FOLDERS: list[Path] = get_all_templates()

    #  fastapi mail stub
    TEMPLATE_FOLDER: Path = Path()

    def template_engine(self) -> Environment:
        loader = FileSystemLoader(self.TEMPLATE_FOLDERS)
        return Environment(loader=loader)


class Mailer:
    def __init__(self) -> None:
        self.conf = MultiTemplateConnectionConfig(
            MAIL_USERNAME=settings.mail.username,
            MAIL_PASSWORD=settings.mail.password,
            MAIL_FROM=settings.mail.from_email,
            MAIL_PORT=settings.mail.port,
            MAIL_SERVER=settings.mail.server,
            MAIL_STARTTLS=settings.mail.mail_starttls,
            MAIL_SSL_TLS=settings.mail.mail_ssl_tls,
            USE_CREDENTIALS=settings.mail.use_credentials,
            VALIDATE_CERTS=settings.mail.validate_certs,
        )
        self.fast_mail = FastMail(self.conf)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions