3
Right now I am using below code:

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'to address'
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.HTMLBody = '<h2>HTML Message body</h2>'# this field is optional
**mail.Attachments.Add('C:\Users\MA299445\Downloads\screenshot.png')**
mail.Send()

I am able to attach a picture but I want to paste this picture in e-mail body.

Thanks in advance

Mayank Shivhare
  • 51
  • 1
  • 1
  • 6

2 Answers2

4

You can use <img> HTML tag:

encoded_image = base64.b64encode(image_file.getvalue()).decode("utf-8")
html = '<img src="data:image/png;base64,%s"/>' % encoded_image

And you can put the tag inside your HTML content.

Don't forget to import required modules:

import base64
Mahdi Perfect
  • 364
  • 3
  • 7
0

Create an attachment and set the PR_ATTACH_CONTENT_ID property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F") using Attachment.PropertyAccessor.SetProperty.

Your HTML body (MailItem.HTMLBody property) would then need to reference that image attachment through the cid:

<img src="cid:xyz"/>

where xyz is the value of the PR_ATTACH_CONTENT_ID property.

Look at an existing message with OutlookSpy (I am its author) - click IMessage button, go to the GetAttachmentTable tab, double click on an attachment to see its properties.

attachment = mail.Attachments.Add("C:\Users\MA299445\Downloads\screenshot.png")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")
mail.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>"
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78