Create PST Files in Python

When it comes to email management, PST (Personal Storage Table) files play a crucial role in storing and organising email data.

In this guide, we’ll explore how to create new PST files in Python using a progressive API. We’ll cover the creation of an empty storage, adding folders and subfolders, incorporating standard and regular folders, and finally, adding emails and Outlook items to the PST file.

Python API to Create and Populate PST Files

To work with storage files in PST format in Python, we will use Aspose.Email for Python. It is a powerful API that provides developers with the tools to work with email formats, including PST files, in their Python applications. With Aspose.Email, you can easily create, manipulate, and manage email messages and storage files, making it an essential tool for email-related tasks.

To leverage the power of the API, you can download its DLL or install it from PyPI

Create a new PST file

A new PST file can be easily generated with the PersonalStorage.create() method. The following code snippet demonstrates how to create a new storage file named “output.pst” using Aspose.Email:

  1. Import the necessary modules from Aspose.Email.
  2. Define the path for the new PST file.
  3. Create a new empty PST file using the ‘PersonalStorage.create()’ method, passing the file path and specifying the Unicode format. This creates a blank file ready for further customization, such as adding folders, messages, or contacts.

Note: the minimum pst is 256 Kb, and contains a root folder and a Deleted Items folder.

Adding Folders and Subfolders

Once we have a new empty storage file, the next step is to add folders and subfolders to organize our data efficiently. Consider the following subsections and try the following code samples to perform the task.

Adding Standard Folders

It’s a common practice to include standard folders such as Inbox, Sent Items, Contacts, or Calendar when working with PST files. Aspose.Email for Python simplifies the task by providing easy-to-use methods. The following code snippet demonstrates how to add standard folders to a storage file using the Python API:

  1. Create a new PST file using the PersonalStorage.create() method as shown above.
  2. Create a standard Inbox folder using the create_predefined_folder() method, passing the name “Inbox” and specifying the type as StandardIpmFolder.INBOX. This ensures that the file contains a standard Inbox folder ready for use.

Standard folders like Deleted Items, Calendar, Contacts, Journal, Notes, Tasks, Sync issues, Junk emails, RSS feeds can be easily added to a new PST file:

Standard Folders

Adding Regular Folders

In addition to standard folders like Inbox and Sent Items, Aspose.Email for Python allows you to create custom or regular folders within PST files. They provide flexibility in organizing and categorizing email data according to specific requirements. The following code sample demonstrates how to add regular folders using Aspose.Email:

  1. Create a new storage using the PersonalStorage.create() method.
  2. Use the add_sub_folder() method to create a custom folder named “SomeFolder” within the root folder of the PST file.
  3. Create a subfolder named “SubFolder” within the “SomeFolder”, providing a hierarchical structure to organize email data effectively.

After creating the custom folder, you can use the ‘add_sub_folder()’ method again to create subfolders within it. Provide the desired name for the subfolder as a parameter to the method.

By incorporating regular folders into PST files, developers can tailor the email storage architecture to suit specific application needs.

Adding Emails and Outlook Items

With the structure in place, we can now populate our PST file with emails and other Outlook items. This involves creating emails, appointments, contacts, tasks, and more, and adding them to the appropriate folders within the file. Let’s consider a couple of examples.

Adding New Emails

The code sample below will show you how to add a new email message to the “Inbox” folder within the PST file. The code involves the following steps:

  1. Create a new storage named “sample.pst” with Unicode format using the PersonalStorage.create() method.
  2. Access the “Inbox” folder within the storage file using the get_sub_folder() method.
  3. Create a new email message using the MapiMessage() constructor, specifying the sender, recipient, subject, and body of the email.
  4. Add the newly created message to the “Inbox” folder within the PST file using the add_message() method.

Adding Outlook Items

The following code sample allows you to add a contact to a new PST file as an example. It outlines the process of creating a new personal storage file, adding a standard Contacts folder to it, loading a contact from a VCF file, and adding the contact to the Contacts folder. The code involves the following steps:

  1. Create a new storage named “sample.pst” with Unicode format using the PersonalStorage.create() method.
  2. Create a standard folder named “My Contacts” within the storage file using the create_predefined_folder() method, specifying the type as StandardIpmFolder.Contacts.
  3. Load a contact from a VCF (vCard) file using the from_v_card() method. This method parses the VCF file and generates a MapiContact object representing the contact information.
  4. Add the contact to the “My Contacts” folder within the PST file using the add_mapi_message_item() method.

Conclusion

In this tutorial, we’ve explored how to create new PST files in Python using a progressive API provided by Aspose.Email. We’ve covered various aspects, including creating empty PST storage, adding folders and subfolders, incorporating standard and regular folders, and adding emails and Outlook items to the storage file.

With Aspose.Email for Python via .NET, creating PST files programmatically becomes a straightforward task. Try it out in your Python projects and streamline your email-related workflows today!

Apart from that, the library offers a wide range of features for creating, extracting, parsing, converting, and manipulating emails supported by comprehensive documentation and code examples to streamline the integration process, making it easy for developers to implement email-related solutions within their .NET applications.

Along with the advanced API, you can get continuous access to free resources:

  • support forum - a huge community to obtain answers to all of your questions
  • blog - invaluable information about up-to-date features
  • documentation - tutorials, code examples, and best practices with access to API references
  • free apps to view or convert, merge or parse emails, analyze headers, add watermark, and more

Aspose.Email ensures that developers have the resources needed to navigate any challenges and maximize the potential of the API within their applications.

See Also