{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/exporting-email-threads-from-gmail-into-csv-file","result":{"data":{"allGhostPost":{"edges":[{"node":{"title":"Exporting Email Threads from Gmail into a CSV File","html":"
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.
\nStart 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.
\nUncheck 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.
\n\nOn 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.
\n\nYou can either browse directly to the Google Takeout downloads page or go through the link in that email.
\n\nDownload 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\nimport csv\n\nwriter = csv.writer(open("mbox-output.csv", "wb"))\n\nfor message in mailbox.mbox('file.mbox/mbox'):\n writer.writerow([message['message-id'], message['subject'], message['from']])\n
\nReplace 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']]\n
\nRun the script. The resulting file will be called mbox-output.csv
.