Exporting Email Threads from Gmail into a CSV File

Thanks to Google Takeout (help page), it is very easy to export all kinds of data from your Google Account. In this case, we want to export certain emails.

Start off by creating a new label and adding it to every email that you want to export. If you want to export all of your emails, you can skip this step. Once you've done that, head over to the Google Takeout page.

Uncheck everything by clicking on the Select None button and scroll down to the Mail section. Turn that toggle on and click on the arrow next to it to expand the option pane. Choose whether you want to export all of your emails or certain labels only.

screenshot

On the next page, select the archive format and delivery that you like. I went with a .tgz file and the email delivery method. A few minutes later, depending on the total size of emails that you are exporting, you should receive an email telling you that the archive is ready.

screenshot

You can either browse directly to the Google Takeout downloads page or go through the link in that email.

screenshot

Download the archive and extract it. In the Takout/Mail folder, you will see an mbox file named after the label you chose. To convert that into a CSV file, we will use this python script.

import mailbox
import csv

writer = csv.writer(open("mbox-output.csv", "wb"))

for message in mailbox.mbox('file.mbox/mbox'):
    writer.writerow([message['message-id'], message['subject'], message['from']])

Replace file.mbox/mbox with the name of the mbox file in Takeout/Mail and place the script in the same directory. You might want to adjust the list of fields. I personally only needed the sender's emails, so mine looked like this:

[message['from']]

Run the script. The resulting file will be called mbox-output.csv.