red-mail icon indicating copy to clipboard operation
red-mail copied to clipboard

issue with embed base64 image into jinja2 template

Open hexkey opened this issue 2 years ago • 1 comments

Hello Mikael, Thank you for making red-mail, it's great helper.

Describe the bug It seems it's not possible to embed base64 image into jinja2 template. Image is always attached to the email instead being embedded.

To Reproduce

Code:

    # picture is passed from python PIL from another function as arg
    # picture = base64.b64encode(buff.getvalue())
    # this way, I avoid saving and reading picture from disk, so base64 is super useful. 

    tp_decoded = base64.b64decode(picture)
    red_email.send(
        subject="The Subject",
        receivers=["[email protected]"],
        html_template="email/reset.html",
        body_images={
            "myimage": tp_decoded,
        },
    )

Template file:

<h1>Hello,</h1>
<p>
    {{ myimage }}
</p>

End result is attached image instead of embedded:

email

Expected behavior Expecting that this would work the same way, as when no template is being used like in this example:

    tp_decoded = base64.b64decode(picture)
    red_email.send(
        subject="The Subject",
        receivers=["[email protected]"],
        html="""
            <h1>Hello,</h1>
            <p>Auth code is valid for 30 minutes</p>
            {{ myimage }}
            """,
        body_images={
            "myimage": tp_decoded,
        },
    )

Without template, image is embedded correctly: email2

Email provider:

  • Email service: Gmail, Protonmail
  • Application to view the email: Web Gmail, Web Protonmail

Environment (please complete the following information if relevant):

  • OS: Linux
  • Python version [3.10.5]
  • Red Mail version [0.4.0]

hexkey avatar Jul 18 '22 18:07 hexkey

Why encode and decode again? You should be able to just embed the bytes

if you can pass around the PIL.Image object instead, you should be able to embed that as well

I may have misunderstood you problem, if these dont work please clarify the original type of the image object that is passed to these lines of code

Waghabond avatar Aug 24 '22 07:08 Waghabond