How to Export Emails from an Outlook Account to a CSV File

Anyquery is a SQL query engine that allows you to run SQL queries on virtually anything, including email accounts. In this tutorial, we will show you how to export emails from an Outlook account to a CSV file using Anyquery.

Prerequisites

Before starting, ensure you have the following:

Step 1: Install the IMAP Plugin

First, install the IMAP plugin if you haven't already:

anyquery install imap

Step 2: Create an Application Password

Go to https://account.live.com/proofs/manage and create an application password for Anyquery. Input the name you want and copy the generated password (only if using two-factor authentication).

Step 3: Create a New Profile for the IMAP Plugin

Now, create a new profile for the IMAP plugin. Run the following command:

anyquery profile new default imap outlook

Fill in the following details when prompted:

Step 4: Export Emails to a CSV File

Now that you have set up the profile, you can export your emails to a CSV file using SQL. Here is an example command to get the subject, sender, and date of the first 100 emails and export them to a CSV file:

anyquery -q "SELECT subject, _from, received_at FROM outlook_imap_emails LIMIT 100" --csv > emails.csv

:::caution Ensure you include the LIMIT clause to avoid fetching an overwhelming number of emails. :::

Additionally, you can customize your export by filtering specific emails. For example, to export only unread emails:

anyquery -q "SELECT subject, _from, received_at FROM outlook_imap_emails WHERE flags NOT LIKE '%\"Seen\"%' LIMIT 100" --csv > unread_emails.csv

Conclusion

You have successfully exported emails from your Outlook account to a CSV file using Anyquery. For more information on available functions and further customization, refer to the functions documentation.