Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hsum opened this issue May 6, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@hsum
Copy link

hsum commented May 6, 2023

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'
        }
    )
@hsum hsum added the enhancement New feature or request label May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant