Search code examples
pythonhtml-emailexchangelib

How to add multiple embed images in a body email using exchangelib


I have a message in HTML format. I need to reply to this email, keeping all the pictures from the previous email.

I have all the pictures from the email saved, and now I need to add them back. How to do it?

exchabgelib has instructions for this:

from exchangelib import HTMLBody

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)

# Most email systems

message.body = HTMLBody(
'<html><body>Hello logo: <img src="cid:%s"></body></html>' % logo_filename
)

Solution

  • If the new email must also be in HTML format, then attach the images one by one and add an <img> tag for each image you have attached. Otherwise, just attach the images and compose the email in plain text.