Convert MSG and EML files to PDF in Python

Emails to PDF conversions are needed while referencing and sharing the email content. In this article, we will learn to convert email message files like MSG and EML into PDF using Python. This will help you to automate the conversion of email messages on the cloud within your application.

Convert Emails to PDF in Python

The following are the topics covered in this article:

Python Conversion Library

I will be using GroupDocs.Conversion Cloud API for Python for the conversion of EML and MSG email messages to PDF on the cloud. By using this API, you may also convert a large list of document and image formats into any other supported format.

There are python examples available on GitHub that help you learn and implement the features in your own application. You may install groupdocs-conversion-cloud with pip (package installer for python) from PyPI (Python Package Index) using the following command:

pip install groupdocs-conversion-cloud

or clone the repository and install it via setuptools:

python setup.py install

Before you proceed, quickly get your Client ID and Client Secret from your dashboard and then jump below to see the python way to convert your emails into PDF which is the popular portable document format.

Convert MSG to PDF using Python

Outlook MSG files can be converted to PDF with just a few lines of code and following the below-mentioned steps. Embedded links in the steps will allow further exploring the classes and methods.

  1. Set the configuration using the Client ID, Client Secret, and API base URL.
  2. Configure the settings with the file path and output format.
  3. Set loading options using EmailLoadOptions.
  4. Use convert_document method along with the settings to convert the MSG file to PDF format.

The following python code follows the above steps and converts the email MSG file to PDF format. You also have the option to hide or show different fields (to, cc, bcc) of email messages.

Here is the sample MSG file that is created using Microsoft Outlook. Further below is the PDF file, that is obtained by converting the MSG file using the python code.

MSG Email file to be converted to PDF programmatically
Converted MSG to PDF file programmatically

Convert EML to PDF using Python

Similarly, ee can also programmatically convert our EML format email messages into PDF format with similar lines of python code. The following steps will guide you to achieve the objective.

  1. Set the configuration using the Client ID, Client Secret, and API base URL.
  2. Define the source path, output format, and ConvertSettings.
  3. Set loading options using EmailLoadOptions and also define the fields to show or hide in the converted PDF.

Here are the source EML file and the converted PDF file screenshots, that have been converted using the above code.

EML file to be converted to PDF programmatically
Converted EML to PDF file programmatically

Conclusion

Today, we learned to convert the MSG and EML files to PDF on the cloud using Python Conversion API. Furthermore, we can programmatically apply customization to resultant PDF files to get the outcome in our desired style. You may learn more about GroupDocs.Conversion Cloud API from the documentation. In case of any ambiguity, feel free to contact support.

See Also