How to Convert Outlook Email to TIFF using Python

In this small topic, we will focus on how to convert Outlook Email to TIFF using Python by following the detailed steps for setting up the environment. In order to convert an EML to TIFF Python based API providing a simple API interface can be used in any Python configured environment in operating systems like Windows or Linux.

Steps to Convert Outlook Email to TIFF using Python

  1. Set up the environment to use Aspose.Email for Python via .NET and Aspose.Words for Python via .NET in your application to convert Email to TIFF in Python
  2. Create the MailMessage class object to load the MSG or EML format email file
  3. Save the accessed Email (EML/MSG) file in a memory stream to an MHTML format
  4. Set the LoadFormat to MHTML and instantiate the Document class object to load the intermediate MHTML created in the earlier step
  5. Use the save method in Python to perform the EML to TIFF conversion

By performing the aforementioned steps and using the simple code in Python Email to TIFF converter application can be developed. We will start by loading the source MSG or EML file from the disk using an instance of the MailMessage class, which is then followed by saving it to an intermediate MHTML file inside the memory stream. Subsequently, by using an instance of the Document class, the MHTML will be loaded and saved as a TIFF file on the disk using the Save method.

Code to Convert Outlook Email to TIFF using Python

import aspose.email as ae
import aspose.words as aw
import io
# Path to load the source files
srcFilePath = "Y://TestData//"
# Load the license in your application for converting MSG/EML to MHTML
emlFileLicense = ae.License()
emlFileLicense.set_license(srcFilePath + "Conholdate.Total.Product.Family.lic")
# Load the Aspose.Words license in your application for converting MHTML to TIFF
wordsFileLicense = aw.License()
wordsFileLicense.set_license(srcFilePath + "Conholdate.Total.Product.Family.lic")
# Open the source MSG or EML file from the disk
srcMessageFile = ae.MailMessage.load(srcFilePath + "Message.msg")
# Save the document to an intermediate stream as an MHTML with default options
mhtml_int_Stream = io.BytesIO()
# Save the email to stream
srcMessageFile.save(mhtml_int_Stream, ae.SaveOptions.default_mhtml)
# seek the position of the Memory stream to the brginning
mhtml_int_Stream.seek(0)
# Set the LoadOptions to Load the Mhtml
loadOptions = aw.loading.LoadOptions()
loadOptions.load_format = aw.LoadFormat.MHTML
# Initialize the Document class object to load the intermediate MTHML from MemoryStream
mhtmlToTiffDocument = aw.Document(mhtml_int_Stream, loadOptions)
# Close the MHTML stream to avoid a memory leak
mhtml_int_Stream.close()
# Save the MHTML to TIFF using Python
mhtmlToTiffDocument.save(srcFilePath + "SaveEmailAsDoc.tiff")
print ("EML converted to Tiff file")

In order to convert EML to TIFF Python based API exposing simple features and crisply defined steps have been referred to in the above example. It is a multiple steps process whereby in the first step, we will perform the EML or MSG file to an intermediate MHTML file conversion inside a memory stream. In the final step, the intermediate MHTML is rendered as a TIFF file and saved on the disk.

In this example, we learnt about how to convert Outlook Email to TIFF using Python based API. If you are interested in learning about creating a Mapi Contact, refer to the article on how to create Mapi Contact using Python.

 English