Skip to content

Using msg extractor In Your Own Code

Destiny Peterson edited this page Feb 25, 2022 · 10 revisions

Basic Usage

Before anything else, you will first, of course, need to import the module.

import extract_msg

All example codes will be assuming that you have imported the module like so.

Opening and Closing an MSG File

It is highly recommended that you do not open an MSG file directly using a class, but rather use openMsg to do this. This function will automatically determine if the MSG file has support in any form, and if it does, which class to use for reading it.

msg = extract_msg.openMsg('path/to/msg/file.msg')

MSG files can be closed in the same way that a normal file can, simply using the close method of the class.

msg.close()

MSGFile, the base class for all MSG file types, supports the __enter__ and __exit__ magic functions, allowing you to use the with context manager with them. At the end of the with context manager, the file will automatically be closed.

with extract_msg.openMsg('path/to/msg/file.msg') as msg:
    # Do some stuff

Saving

Not all classes support saving. If you try to call a save function when a class doesn't have it, the function will raise a NotImplementedError. Currently the only class that supports saving is the Message class. This function requires no arguments, but it is likely that you will want to use some of them. The function also returns a reference to the current instance, allowing for you to chain certain functions directly.

msg.save()
Clone this wiki locally