extract_msg.attachments.attachment Module

Module contents

class extract_msg.attachments.attachment.Attachment(msg: MSGFile, dir_: str, propStore: PropertiesStore)[source]

Bases: AttachmentBase

A standard data attachment of an MSG file.

Parameters:
  • msg – the Message instance that the attachment belongs to.

  • dir – the directory inside the MSG file where the attachment is located.

  • propStore – The PropertiesStore instance for the attachment.

getFilename(**kwargs) str[source]

Returns the filename to use for the attachment.

Parameters:
  • contentId – Use the contentId, if available.

  • customFilename – A custom name to use for the file.

If the filename starts with “UnknownFilename” then there is no guarantee that the files will have exactly the same filename.

regenerateRandomName() str[source]

Used to regenerate the random filename used if the attachment cannot find a usable filename.

save(**kwargs) Tuple[SaveType, List[str] | str | None][source]

Saves the attachment data.

The name of the file is determined by several factors. The first thing that is checked is if you have provided :param customFilename: to this function. If you have, that is the name that will be used. If no custom name has been provided and :param contentId: is True, the file will be saved using the content ID of the attachment. If it is not found or :param contentId: is False, the long filename will be used. If the long filename is not found, the short one will be used. If after all of this a usable filename has not been found, a random one will be used (accessible from randomFilename()). After the name to use has been determined, it will then be shortened to make sure that it is not more than the value of :param maxNameLength:.

To change the directory that the attachment is saved to, set the value of :param customPath: when calling this function. The default save directory is the working directory.

If you want to save the contents into a ZipFile or similar object, either pass a path to where you want to create one or pass an instance to :param zip:. If :param zip: is an instance, :param customPath: will refer to a location inside the zip file.

Parameters:
  • extractEmbedded – If True, causes the attachment, should it be an embedded MSG file, to save as a .msg file instead of calling it’s save function.

  • skipEmbedded – If True, skips saving this attachment if it is an embedded MSG file.

property data: bytes

The bytes making up the attachment data.

property randomFilename: str

The random filename to be used by this attachment.

property type: AttachmentType

An enum value that identifies the type of attachment.