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

Jinja2 templates not getting populated with body #46

Closed
fletcheaston opened this issue Feb 2, 2021 · 8 comments
Closed

Jinja2 templates not getting populated with body #46

fletcheaston opened this issue Feb 2, 2021 · 8 comments

Comments

@fletcheaston
Copy link

I'm trying to send an HTML rendered email with Jinja2 templates, but no context is being send.

Here's an example of the message I'm sending...

message = MessageSchema(
    subject="Grove Password Reset",
    recipients=[form.email],
    body={"email": email, "other": "some random string"},
    subtype="html",
)

fast_mail = FastMail(settings.EMAIL_CONFIG)

background_tasks.add_task(fast_mail.send_message, message, template_name="template.html")

The email is sent correctly, and the template shows up as a rendered HTML email, but none of the information from the body parameter gets rendered.

@fletcheaston
Copy link
Author

Temporary solution is to use f-strings. Fortunately, setting the subtype to html still works to render those strings as html.

@sabuhish
Copy link
Owner

sabuhish commented Feb 5, 2021

@maestro-1 has done a great job on that. #21 has an example as well. What you are missing is specifying TEMPLATE_FOLDER in your config.

@wajeshubham
Copy link

wajeshubham commented Feb 20, 2021

you can access body dict keys in your html template as follows:

{{ body.other }}

it will render "some random string" in your template

@sabuhish
Copy link
Owner

Thanks @wajeshubham for the explanation as well! As I see we gave examples, it should be now clear. if you have other issues please re-open the new issue.

@Nikhilbk1705
Copy link

Thanks @wajeshubham for the explanation as well! As I see we gave examples, it should be now clear. if you have other issues please re-open the new issue.

Hello Sir, I am trying to render html inside the the body using f string email is sent correctly but in the body, the html is displayed as the plain text along with html element as attached in below screenshot
issue

@Nikhilbk1705
Copy link

Hello Sir, I am trying to render html inside the the body using f string email is sent correctly but in the body, the html is displayed as the plain text along with html element as attached in below screenshot

Hello Sir, I am trying to render html inside the the body using f string email is sent correctly but in the body, the html is displayed as the plain text along with html element as attached in below screenshot
issue

@Nikhilbk1705
Copy link

Hello Sir, I am trying to render html inside the the body using f string email is sent correctly but in the body, the html is displayed as the plain text along with html element as attached in below screenshot

Hello Sir, I am trying to render html inside the the body using f string email is sent correctly but in the body, the html is displayed as the plain text along with html element as attached in below screenshot issue

This can be solved by using "html" field instead of "body " in message scheme

@reinaldoBloopark
Copy link

@Nikhilbk1705 can you show me an example?
Do you mean:
message = MessageSchema(
subject=subject,
recipients=[email_to],
html="

Thanks

",
subtype='html',
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants