Neil
Neil

Reputation: 13

Python how to insert images using win32com.client and outlook?

I'm trying to send html emails using win32com.client. Here's a sample code:

import win32com.client as win32  

mail = win32.Dispatch('outlook.application').CreateItem(0)
mail.To = '[email protected]'
mail.Subject = 'test'
mail.HTMLBody = html_pages

mail.Send()

My solution now is to upload the images to a server and insert the urls into html_pages. The drawback is that the images are not displayed when the server is down. Is there a way to send the images with the emails using win32com?

Upvotes: 1

Views: 19490

Answers (1)

Piotrek
Piotrek

Reputation: 1530

One way to do that would be sending images as an attachment:

import win32com.client as win32  

mail = win32.Dispatch('outlook.application').CreateItem(0)
mail.To = '[email protected]'
mail.Subject = 'test'
mail.HTMLBody = html_pages

attachment  = '*path to your image*'
mail.Attachments.Add(attachment)
mail.Send()

Or you can put image in the html body:

import win32com.client as win32  

mail = win32.Dispatch('outlook.application').CreateItem(0)
mail.To = '[email protected]'
mail.Subject = 'test'
mail.HTMLBody = html_pages + <br><img src="path">

mail.Send()

Or if you want the image actually attached to the body and not being linked to a path, have a look here:

I am not able to add an image in email body using python , I am able to add a picture as a attachment but i want a code to add image in mailbody

Upvotes: 2

Related Questions