Mayank Shivhare
Mayank Shivhare

Reputation: 71

Add an image in email body using Python

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 file but I want a picture in the e-mail body.

Upvotes: 4

Views: 13328

Answers (3)

Zhijian Jim Luo
Zhijian Jim Luo

Reputation: 87

One step further to Dmitry's answer, I write a completed version to send out outlook email.

def outlook_attach_one_image_inline(mail, filePath, i) :
    attachment = mail.Attachments.Add( filePath )
    imageId = 'MyImage%d'%i
    attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", imageId)
    return "<br/><img src=""cid:" + imageId +">"

def outloook_attach_images_to_msg_inline(mail, images)     :
    i = 1
    htmlForImages = ''
    for f in images or []:
        if not f:
            continue
        htmlForImages += outlook_attach_one_image_inline(mail, f, i)
        i +=1
    return htmlForImages
    
def outloook_attach_files_msg(mail, files)     :
     for f in files or []:
        if not f:
            continue
        mail.Attachments.Add( f )

def  outlook_send_email_with_images_attachments  (to, subject, html_str, images, files, cc)  :
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = to.replace(',',';')
    if cc :
        cc2= cc.replace(',',';')
        mail.CC  = cc2
    mail.Subject = subject
    
    html_str=''  #more message here
    html_str += outloook_attach_images_to_msg_inline (mail, images)
    outloook_attach_files_msg (mail, files)
  
    mail.HTMLBody = html_str  
    mail.Send()    

Upvotes: 1

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66265

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>"

Upvotes: 1

Mahdi Perfect
Mahdi Perfect

Reputation: 364

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

Upvotes: 4

Related Questions