Skip to content

Using msg extractor In Your Own Code

Destiny Peterson edited this page Nov 30, 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

openMsg takes a filename for its first argument. It can also the raw bytes that would make up an MSG file or a file-like object. File-like objects at minimum will require a read, seek, tell, and close method. The read method MUST return bytes and MUST return at most the number of bytes requested.

Saving

While most of the classes support saving, not all of them do. If you try to call a save function when a class doesn't have it, the function will raise a NotImplementedError. The save 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