0

I am looking to send an image embedded in HTML to send out to a list of senders.

I need to use win32com.client, SMTP is blocked. The image is saved as png and is originally a matplotlib bar chart.

The script successfully sends out the email the recipients cannot see the image embedded in the email. However, I can see the image when I send the email to myself.

I tried to attach the image to the email, still no luck.

email = """<body>
<p><img src="C:\output.png"></p>
</body>
"""

import win32com.client
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "the subject"
newMail.HTMLBody = email
newMail.To = "[email protected];[email protected]"
attachment1 = "C:\output.png"
newMail.Attachments.Add(attachment1)
newMail.Send()

Any help greatly appreciated!

1

3 Answers 3

1

In order to display the image in the mail body, use the below code:

mail.Attachments.Add(file_location + 'test.png')
mail.HTMLBody = "<html><body>  <img src='cid:test.png'>  </body></html>";

Basically if you want to display an attached image in the mail body, you have to refer to it using img src = 'cid:name' , or else it won't be shown.

Sign up to request clarification or add additional context in comments.

Comments

0

The distribution list and I have a shared drive. I saved the file in the shared drive and now the receivers of the message can see the image in the email.

Comments

0

I had a similar issue, sending images through the body. Below I have attached the code that fixed it for me.

Code:

email.Display(False) ;
email.Send()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.