1
outlook = win32com.client as win32
mail = outlook.CreateItem(0)

for j in range(list)
    file = "C:desktop\\image{}.png".format(j)
    attachment = mail.Attachments.Add(file)
    index = "ID{}".format(j)
    attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", index)

mail.HTMLBody = "<html><body> image <img src = ""cid:ID%s""></body></html>" % (j)

mail.Send()

The purpose is to embed all images (0,1,2,...maximum number defined by 'list') into the email. But now stuck at the for loop. May I know if there is any method can achieve this purpose? Thanks!

1 Answer 1

3

After some intense research on the internet, I have found an answear to this particular problem. The solution was to import base64 and generate an image tag with it. I will post here my code so you can try it for yourself.

import win32com.client as win32
import base64

def send_email(subject, text, list_img_paths):

    outlook = win32.Dispatch('outlook.application')
    myItem = outlook.CreateItem(0)

    myItem.Subject = subject
    list_email_img = []
    img_tag = ''
    for i in range(len(list_img_paths)):
        list_email_img.append(base64.b64encode(open(list_img_paths[i], 'rb').read()).decode('utf-8').replace('\n',''))
        img_tag +='<img alt="" src="data:image/png;base64,{}"/>'.format(list_email_img[i])
    myItem.HtmlBody = """\
    <html>
    <head>{}</head>
    <body>
    <p>{}<br>
    </p>
    </body>
    </html>
    """.format(text, img_tag)
    myItem.To = "[email protected]"
    myItem.CC = '[email protected]'
    myItem.Display()
    myItem.Send()

Hope this can help

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.