How to convert an MSG Outlook File to HTML String in Python

Outlook’s MSG format for emails is particularly troublesome to parse, potentially consisting of HTML, RTF, or plain text components. Then you have to consider attachments, images, and other possible issues, and you have yourself a real headache. With the help of an API, however, we can conveniently sidestep the whole problem and move on to more important things, like slacking off! Let’s begin.

First, we will install Cloudmersive’s API client for documents and format conversion.

pip install cloudmersive-convert-api-client

Once pip install is finished doing its thing, we are in a position to call our function:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.body_only = true # bool | Optional; If true, the HTML string will only include the body of the MSG. Other information such as subject will still be given as properties in the response object. Default is false. (optional)include_attachments = true # bool | Optional; If false, the response object will not include any attachment files from the input file. Default is true. (optional)try:# Convert Email MSG file to HTML stringapi_response = api_instance.convert_document_msg_to_html(input_file, body_only=body_only, include_attachments=include_attachments)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_msg_to_html: %s\n" % e)

This function will take in our MSG file, as well as let you optionally specify if you want the sender, subject, etc to be included, and deal with attachments. Pretty good for five minutes of work.

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store