Ariel
Ariel

Reputation: 988

Python win32com.client send image embedded in email

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!

Upvotes: 0

Views: 5549

Answers (3)

Dip Chatterjee
Dip Chatterjee

Reputation: 87

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.

Upvotes: 1

Saikiran
Saikiran

Reputation: 21

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()

Upvotes: 0

Ariel
Ariel

Reputation: 988

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.

Upvotes: 0

Related Questions