Sample code for 30+ languages & platforms
CkPython 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 CkPython Downloads

CkPython
import sys
import chilkat

success = False

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

email = chilkat.CkEmail()
success = email.LoadEml("with-attachments.eml")
if (success != True):
    print(email.lastErrorText())
    sys.exit()

preview = chilkat.CkEmail()
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.get_NumAttachments()) + "  Files in preview: " + str(preview.get_NumAttachments()))
if (preview.HasHtmlBody() != True):
    print("This message has no HTML preview.")
    sys.exit()

html = chilkat.CkStringBuilder()
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.")