2

I need to embed multiple images into an email's body. I have tried below code but it does not embed it to the email's body. I am not looking to make it as an attachment

with open(i, 'rb') as f:
    my_logo = FileAttachment(
    name=i,
    content=f.read(),
    is_inline=True,
    content_type='GIF/Image',
    content_id=i,
)
m.attach(my_logo)

Thanks

Jonathan Lam
  • 1,237
  • 3
  • 20
  • 49

1 Answers1

2

Check out the example in the exchangelib documentation: https://ecederstrand.github.io/exchangelib/#attachments

In addition to creating the attachment, you need to reference it in your HTML body:

message = Message(...)
logo_filename = 'logo.png'
with open(logo_filename, 'rb') as f:
    my_logo = FileAttachment(
        name=logo_filename, content=f.read(),
        is_inline=True, content_id=logo_filename,
    )
message.attach(my_logo)
message.body = HTMLBody(
    '<html><body>Hello logo: <img src="cid:%s"></body></html>' % logo_filename
    )
Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63