Skip to content

ENH: add parameter to email.send() to allow rendered html to be modified before being emailed #80

@hsum

Description

@hsum

Is your feature request related to a problem? Please describe.
I would like to modify the html after it is rendered by Jinja and before it is emailed. This is useful to minifty-html, cutting down payload size, and/or running premailer to help with converting css to inline styles on elements. This approach would also allow me to provide css as a jinja context variable, providing more flexibility in the final result.

Describe the solution you'd like
email.send() could accept an optional parameter 'finalized_html' that would be a callable. The callable would be used after jinja render(), but before the sender emails out the payload. Please see code below.

Describe alternatives you've considered
I've solved this by rendering the body html and then passing that to email.send(). Unfortunately, this means I need to create the jinja env, compile a Template and then let jinja render the html only to have red-mail repeat these steps. I'm still required to send the context (body_params) because the text template still needs it. Please see code below.

Additional context

from jinja2 import Environment, select_autoescape
from redmail import gmail
from minify_html import minify
from markupsafe import Markup
from premailer import Premailer

if __name__ == '__main__':
    premailer_ = Premailer(
        disable_validation=True,
        remove_classes=True,
    )
    transform = premailer_.transform

    jinja2env = Environment(
        autoescape=select_autoescape()
    )

    html = """
        <h1>Hi {{ name }}</h1>
    """

    body_params = {
        'name': 'Martin'
    }

    template_html = jinja2env.from_string(html)
    html_ = template_html.render(**body_params)

    gmail.username = '?@gmail.com'
    gmail.password = 'apppassword'
    email = gmail

    email.send(
        subject='email subject',
        receivers=['first.last@example.com'],
        html=minify(transform(html_)),
        text="""
            Hi {{ name }}
        """,
        body_params=body_params,
    )


if __name__ == 'WOULD_BE_NICE__main__':
    premailer_ = Premailer(
        disable_validation=True,
        remove_classes=True,
    )
    transform = premailer_.transform

    gmail.username = '?@gmail.com'
    gmail.password = 'apppassword'
    email = gmail
    
    html = """
        <h1>Hi {{ name }}</h1>
    """
    
    email.send(
        subject='email subject',
        receivers=['first.last@example.com'],
        finalize_html=lamdba html_: minify(transform(html_)),
        html="""
            <h1>Hi {{ name }}</h1>
        """,
        text="""
            Hi {{ name }}
        """,
        body_params={
            'name': 'Martin'
        }
    )

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions