Embed image into outlook email

Viewed 25

I want to embed images saved in local directory into an outlook email. Have defined a function to do that and also added the html according. However, the image doesn't come up. Not an issue with the file path as I've already checked that it's working. What did I do wrong?

def Emailer(text, subject, recipient, cc):
    import win32com.client as win32
    import os

    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.CC = cc
    mail.Subject = subject
    mail.HtmlBody = text
    mail.Display(True)

MailSubject = "Daily Report for " + date_slash
MailInput ="""

<div>
    <img src={}></img>
</div>
<div>
    <p>
</div>
<div>
    <img src={}></img>
</div>
<div>
    <p>
</div>
<div>
    <img src={}></img>
</div>
<div>
    <p>
</div>
<div>
    <img src={}></img>
</div>
<div>
    <p>
</div>
<div>
    <img src={}></img>
</div>
"""

MailInput = MailInput.format(date_slash, sum_path, ovdv_path, cot_path, rub_path ,pnl_path) MailRecipient ="xxx@yyy.com;" MailCc = "xxx@yyy.com;"

1 Answers

A remote recipient cannot obviously see files from your machine.

You need to add images as attachments (Attachment = MailItem.Attachments.Add) and set the PR_ATTACH_CONTENT_ID MAPI property to the value used in the <img> tag (e.g., <img src='cid:MyTestId'>) using Attachment.PropertyAccessor.SetProperty:

Attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E",  "MyTestId")
Related