Read Outlook emails in Python

In certain cases, you have to parse the Outlook emails and extract the information from the messages programmatically. This information includes the sender’s address, the receivers’ addresses, the body, the email’s headers, and so on. In this article, you will learn how to parse and read Outlook email messages in Python. We will also demonstrate how to fetch the headers of an email message programmatically.

Python Library to Read Outlook Emails - Free Download

Aspose.Email for Python is a feature-rich library to create and send emails from within Python applications. Furthermore, it lets you parse the email messages and fetch their content. We will use this library to read Outlook emails in this article. You can download the library or install it using the following command.

> pip install Aspose.Email-for-Python-via-NET

Read an Outlook Email Message in Python

Aspose.Email for Python allows reading all the essential data from an Outlook email file such as sender, recipients, email body, etc. Let’s see how to read this data from an email in Python.

  • Load email file using MailMessage.load(fileName) method.
  • Read the data using properties such as subject, body, html_body, etc.

The following code sample shows how to read an Outlook email in Python.

Extract Plain Text from Email’s HTML Body

Sometimes you need to extract the plain text from the HTML body of the email. For this, you can simply use MailMessage.get_html_body_text(False) method. The following code sample shows how to read the plain text from the HTML body of an email in Python.

Read Headers of an Email in Python

Aspose.Email for Python also allows you to fetch the header information from an email message. The following are the steps to perform this operation in Python.

  • Load email file using MailMessage.load(fileName) method.
  • Loop through the header values in MailMessage.headers property.
  • Read each value in headers using MailMessage.headers.get(index) method.

The following code sample shows how to read the headers of an email in Python.

Python Library to Read Emails - Get a Free License

You can use Aspose.Email for Python without evaluation limitations by getting a free temporary license.

Python Email Library - Read More

To explore more about Aspose.Email for Python, you can visit documentation. Also, in case of any questions or queries, feel free to let us know via our forum.

Conclusion

In this article, you have learned how to read the content of Outlook email messages programmatically in Python. Moreover, you have seen how to fetch the headers of an email in Python. You can easily install the library and integrate these features into your applications.

See Also