0

Here is my python code to convert my .png image to base64 encoding:

import base64
email_img = base64.b64encode(open('rmh.png', 'rb').read()).decode('utf-8').replace('\n','')                                                                                      
img_tag = '<img alt="" src="data:image/png;base64,{}"/>'.format(email_img)

This code is running without error and is properly storing the base64 encoding in the email_img variable. However, I have been unable to display the image in an outlook email. Here is an abbreviated version of my code relevant to the image:

import win32com.client as win32

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = email[0]
mail.Subject = "UTAS Job Matches"
mail.HtmlBody = """\
<html>
<head></head>
<body>
<p>{}<br>
</p>
</body>
</html>
""".format(img_tag)
mail.Display(True)

I have tried both .format and the %s method for inserting the variables and neither work. The resulting email shows a box with a red X where the image should be. I have not had any trouble with the text I am displaying in my email.

Christian
  • 7
  • 3
  • Why are you assuming that outlook even supports images in base64? – DeepSpace Jan 18 '18 at 19:36
  • @DeepSpace I looked up how others have embedded images into outlook and found this method as a way it is done. It is possible, I guess, that these people could be wrong. – Christian Jan 18 '18 at 19:39
  • @Christian I wrote a [similar solution](https://stackoverflow.com/questions/48272100/embed-an-image-in-html-for-automatic-outlook365-email-send/48311811#48311811) just a bit ago. You're correct that base64 is the correct encoding. However, there's a built in MIME library in smtplib that handles encoding correctly. Also, based on your code snippet, you appear to be missing mime content id headers for your image. – Spence Wetjen Jan 18 '18 at 19:45
  • Take a look at https://www.campaignmonitor.com/blog/email-marketing/2013/02/embedded-images-in-html-email/. There are hurdles and it's messy but _might_ work... but probably not with Outlook. The article mentions that even if you are successful in embedding it, Outlook will very likely not even attempt to download the embedded image. –  Jan 18 '18 at 19:45

0 Answers0