lavr / python-emails

Modern python library for emails
http://python-emails.readthedocs.org
Other
400 stars 54 forks source link

Embedded images problem when using emails.template #122

Closed fdemello closed 5 years ago

fdemello commented 5 years ago

When using jinja from python-emails, if I pass a list of image files to embed to an e-mail, the images don't render. Sample code:

import os
import emails
from emails.template import JinjaTemplate as T

SMTP_SERVER = "my.server.com"
SMTP_PORT = 25
SMTP_SSL = False

JINJA_TEMPLATE = """
{% for image in image_files %}
    <p>This is image {{ loop.index0 }}: {{ image }}</p>
    <img src='{{ image }}'><br/>
{% endfor %}
"""

image_files = []

smtp_info = {
    "host": SMTP_SERVER, 
    "port": SMTP_PORT, 
    "ssl": SMTP_SSL
}

message = emails.Message(html=T(JINJA_TEMPLATE),
                         subject="Test multiple images in e-mail",
                         mail_from="dont-reply@my.server.com")

# remove spaces from files
path  = os.getcwd()
for f in os.listdir("."):
    if os.path.isfile(f) and f.lower().endswith(".png"):
        os.rename(os.path.join(path, f), os.path.join(path, f.replace(' ', '_')))

for f in os.listdir("."):
    if os.path.isfile(f) and f.lower().endswith(".png"):
        image_files.append(f)
        message.attach(filename=f, data=open(f, 'rb'))
        message.attachments[f].is_inline = True

message.transformer.synchronize_inline_images()
message.transformer.save()

doc = {
    "image_files": image_files,
}

r = message.send(to="user@my.server.com", render=doc, smtp=smtp_info)

This results in the e-mail looking like this:

image

If, instead, I use regular jinja and render the HTML e-mail prior to sending, everything works as expected.

from jinja2 import Template
t = Template(JINJA_TEMPLATE)
html = t.render(doc)

message = emails.Message(html=html, 
                         subject="Test multiple images in e-mail", 
                         mail_from="dont-reply@my.server.com")
# same code as above

Version: 0.5.15

aanriot commented 5 years ago

Hello @fdemello,

Could you try with:

<img` src='cid:{{ image }}'><br/>
fdemello commented 5 years ago

@aanriot That did not fix the problem. The difference is that I now don't see anything whereas before I saw broken images.

lavr commented 5 years ago

@fdemello you really could add cid: to src, but also remove line message.transformer.synchronize_inline_images()

fdemello commented 5 years ago

@lavr indeed, that works. Would you say that it's working as designed?

lavr commented 5 years ago

Actually there was no design for this case with custom list of embedded images. But I think it's okay to learn some email internals in such case.

fdemello commented 5 years ago

Glad to hear we have a working method. I’ll close the issue. Thanks!