Sample code for 30+ languages & platforms
Python Requires Chilkat v11.0.0+

Create an HTML preview with embedded images

See more Email Object Examples

Make an independent copy of with-attachments.eml, remove ordinary file attachments from the copy, and export its HTML with related images as data URLs. The original retains its attachments. Use a sanitized viewer when showing untrusted HTML.

Chilkat Python Downloads

Python
import sys
import chilkat2

success = False

# Assumes Chilkat is unlocked. See Global Unlock Sample.

email = chilkat2.Email()
success = email.LoadEml("with-attachments.eml")
if (success != True):
    print(email.LastErrorText)
    sys.exit()

preview = chilkat2.Email()
success = email.MakeCopy(preview)
if (success != True):
    print(email.LastErrorText)
    sys.exit()

# Keep related items: the HTML still refers to them.
preview.DropAttachments()
print("Files in original: " + str(email.NumAttachments) + "  Files in preview: " + str(preview.NumAttachments))
if (preview.HasHtmlBody() != True):
    print("This message has no HTML preview.")
    sys.exit()

html = chilkat2.StringBuilder()
success = preview.GetHtmlBodySb(True,html)
if (success != True):
    print(preview.LastErrorText)
    sys.exit()

success = html.WriteFile("preview.html","utf-8",False)
if (success != True):
    print("Failed: html.WriteFile.")
    sys.exit()

print("Saved preview.html.")