{"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.

\n

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.

\n

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.

\n
\"screenshot\"
\n

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.

\n
\"screenshot\"
\n

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

\n
\"screenshot\"
\n

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.

\n
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
\n

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:

\n
[message['from']]\n
\n

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

\n","published_at":"2017-12-13T21:39:57.000+02:00","slug":"exporting-email-threads-from-gmail-into-csv-file","tags":[],"plaintext":"Thanks to Google Takeout [https://takeout.google.com/settings/takeout] (help\npage [https://support.google.com/accounts/answer/3024190?hl=en]), it is very\neasy to export all kinds of data from your Google Account. In this case, we want\nto export certain emails.\n\nStart off by creating a new label and adding it to every email that you want to\nexport. If you want to export all of your emails, you can skip this step. Once\nyou've done that, head over to the Google Takeout\n[https://takeout.google.com/settings/takeout] page.\n\nUncheck everything by clicking on the Select None button and scroll down to the\n Mail section. Turn that toggle on and click on the arrow next to it to expand\nthe option pane. Choose whether you want to export all of your emails or certain\nlabels only.\n\nOn the next page, select the archive format and delivery that you like. I went\nwith a .tgz file and the email delivery method. A few minutes later, depending\non the total size of emails that you are exporting, you should receive an email\ntelling you that the archive is ready.\n\nYou can either browse directly to the Google Takeout downloads page\n[https://takeout.google.com/settings/takeout/downloads] or go through the link\nin that email.\n\nDownload the archive and extract it. In the Takout/Mail folder, you will see an\n mbox file named after the label you chose. To convert that into a CSV file, we\nwill use this python script\n[https://gist.github.com/davidpelayo/139db134ca2337e966bb].\n\nimport 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\n\nReplace file.mbox/mbox with the name of the mbox file in Takeout/Mail and\nplace the script in the same directory. You might want to adjust the list of\nfields. I personally only needed the sender's emails, so mine looked like this:\n\n[message['from']]\n\n\nRun the script. The resulting file will be called mbox-output.csv.","meta_description":null}}]}},"pageContext":{"slug":"exporting-email-threads-from-gmail-into-csv-file","prev":"publishing-screenshots-screencasts-and-other-files-on-digitalocean-spaces","next":"using-a-linux-server-for-windows-10-backups"}},"staticQueryHashes":["3649515864"]}