{"id":26297,"date":"2016-07-28T14:36:33","date_gmt":"2016-07-28T12:36:33","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=26297"},"modified":"2019-02-01T15:10:07","modified_gmt":"2019-02-01T13:10:07","slug":"exporting-messages-from-gmail-with-fetchmail-and-procmail","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2016\/07\/28\/exporting-messages-from-gmail-with-fetchmail-and-procmail\/","title":{"rendered":"Exporting messages from Gmail with fetchmail and procmail"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>One of the projects that I am involved in has a requirement of importing all the historical emails from a number of Gmail accounts into another system. &nbsp;It&#8217;s not the most challenging of tasks, but since I spent a bit of time on it, I figured I should blog it here too, just in case a similar need will arise in the future.<\/p>\n<p>In my particular case, I need two different solutions. &nbsp;One for exporting all of the messages from all folders of all Gmail accounts in question (<a href=\"https:\/\/apps.google.com\/products\/gmail\/\">Gmail for Work<\/a>). &nbsp;And the other is for exporting only the messages from the &#8220;<em>Sent Mail<\/em>&#8221; folder, which were sent on specific dates.<\/p>\n<p>The solution that I derived is based on the classic tools for this purpose &#8211; <a href=\"http:\/\/www.fetchmail.info\/\">fetchmail<\/a> and <a href=\"http:\/\/www.procmail.org\/\">procmail<\/a>. &nbsp;Fetchmail is awesome at fetching emails using all kinds of protocols. &nbsp;Procmail is amazing at sorting, filtering, and otherwise processing the email messages.<\/p>\n<p>So, here we go. &nbsp;First of all, we need to tell fetchmail where to get the messages from. &nbsp;I didn&#8217;t want to create to separate configurations for each of my tasks, so I left only the options common between them in the configuration file, and the rest I will be passing as command line arguments, depending on scenario.<\/p>\n<p>Note that I&#8217;ve been running these tests from a dedicated environment, where I only had the <em>root<\/em> user. &nbsp;You don&#8217;t have to run it as root &#8211; it&#8217;ll work as any other just fine. &nbsp;Also, keep in mind that I used &#8220;<em>\/root\/fetchmail-test\/<\/em>&#8221; folder for my test runs. &nbsp;You might need to adjust the paths if you have it any different.<\/p>\n<p>Here&#8217;s my <em>fetchmail.rc<\/em> file, which I used to test a single mailbox. &nbsp;A new &#8220;poll&#8221; section will go into this file later, for each mailbox that I&#8217;ll need to export.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npoll imap.gmail.com proto imap:\nusername &quot;someuser@gmail.com&quot; is root here\npassword &quot;somepass&quot;\nfetchall\nkeep\nssl\n<\/pre>\n<p>If you are not root, you might need to adjust the second line, replacing &#8220;root&#8221; with your username. Also, for testing purposes, you can use &#8220;<em>fetchlimit 1<\/em>&#8221; instead of &#8220;<em>fetchall<\/em>&#8220;.<\/p>\n<p>Now, we need two configuration files for procmail. &nbsp;The first one is super simple &#8211; I&#8217;ll use this for simply pushing all downloaded messages into a single giant mbox&nbsp;file. &nbsp;Here&#8217;s the <em>procmail-all.rc<\/em>:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nVERBOSE=0\nDEFAULT=\/root\/fetchmail-test\/fetchmail.all.mbox\n<\/pre>\n<p>As you can see, it only defines the verbosity level and the default mailbox. &nbsp;The second configuration file is a bit more complicated. &nbsp;I&#8217;ll use it for the sent items only. &nbsp;The sent items folder limit will be done with fetchmail. &nbsp;But I want to do further is disregard all messages, which were not sent on a specific date. &nbsp;Here is my <em>procmail-sent.rc<\/em>:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nVERBOSE=0\nDEFAULT=\/dev\/null\n:0\n* ^Date: .*28 Jul 2016.*|\\\n^Date: .*27 Jul 2016.*\n\/root\/fetchmail-test\/fetchmail.sent.mbox\n<\/pre>\n<p>Again, we have the verbosity level and the default mailbox to save messages to. &nbsp;Since I want to disregard them unless they match a certain condition, I specify <em>\/dev\/null<\/em>. &nbsp; Then, I specify my condition, which is simply a bunch of regular expressions for the Date header. &nbsp;Usually, Date header is a not very reliable as different MUAs (Mail User Agents) use different formats, time zones, etc. &nbsp;In this particular case&nbsp;test results seemed consistent (maybe Gmail fixes the header), and I didn&#8217;t have any other more reliable criteria to use.<\/p>\n<p>As you can see, I use a very basic condition for date matching. So, if the Date header matches either &#8220;<em>28 Jul 2016<\/em>&#8221; or &#8220;<em>27 Jul 2016<\/em>&#8220;, the message is saved in the mbox file, rather than being thrown into the default mailbox.<\/p>\n<p>Now, all I need is a way to tie fetchmail and procmail together, as well as provide some additional options. &nbsp;For that I created the two one-liner shell scripts, just so that I won&#8217;t need to figure out the command line arguments&nbsp;if I look at this whole thing six month later.<\/p>\n<p>Here is the check-all.sh script (multi-line for readability):<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\nfetchmail -f fetchmail.rc \\\n-r &quot;&#x5B;Gmail]\/All Mail&quot; \\\n--mda &quot;procmail \/root\/fetchmail-test\/procmail-all.rc&quot;\n<\/pre>\n<p>and here is the check-sent.sh script (multi-line for readability):<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\nfetchmail -f fetchmail.rc \\\n-r &quot;&#x5B;Gmail]\/Sent Mail&quot; \\\n--mda &quot;procmail \/root\/fetchmail-test\/procmail-sent.rc&quot;\n<\/pre>\n<p>If you run either one of these scripts, you&#8217;ll see the output similar to this:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\n$ .\/check-all.sh\nfetchmail: WARNING: Running as root is discouraged.\n410 messages for someuser@gmail.comat imap.gmail.com (folder &#x5B;Gmail]\/All Mail).\nreading message someuser@gmail.com@gmail-imap.l.google.com:1 of 410 (446 header octets) (222 body octets) not flushed\nreading message someuser@gmail.com@gmail-imap.l.google.com:2 of 410 (869 header octets) (230 body octets) not flushed\nreading message someuser@gmail.com@gmail-imap.l.google.com:3 of 410 (865 header octets) (230 body octets) not flushed\n...\n<\/pre>\n<p>Here are a few resources that you might find helpful:<\/p>\n<ul>\n<li><a href=\"https:\/\/support.google.com\/mail\/answer\/82367?hl=en\">Gmail folders in your mail client<\/a><\/li>\n<li><a href=\"http:\/\/www.fetchmail.info\/fetchmail-man.html\">Fetchmail manual<\/a><\/li>\n<li><a href=\"http:\/\/www.zer0.org\/procmail\/quickref.html\">Procmail Quick Reference Guide<\/a><\/li>\n<li><a href=\"https:\/\/www.howtoforge.com\/procmail_tips_recipes\">Jari&#8217;s Procmail Tips Page<\/a><\/li>\n<\/ul>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>One of the projects that I am involved in has a requirement of importing all the historical emails from a number of Gmail accounts into another system. &nbsp;It&#8217;s not the most challenging of tasks, but since I spent a bit of time on it, I figured I should blog it here too, just in case &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2016\/07\/28\/exporting-messages-from-gmail-with-fetchmail-and-procmail\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exporting messages from Gmail with fetchmail and procmail<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Exporting messages from Gmail with fetchmail and procmail #gmail #imap #backup #SysAdmin #mail","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,6,133,62,1334],"tags":[251,1960,72,1985,3318],"keyring_services":[],"class_list":["post-26297","post","type-post","status-publish","format-standard","hentry","category-general","category-linux","category-sysadmin","category-technology","category-web-work","tag-backup","tag-command-line","tag-email","tag-google-gmail","tag-qobo"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":26001,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/04\/06\/sugarcrm-roundcube-and-request-tracker-integration-on-a-single-domain\/","url_meta":{"origin":26297,"position":0},"title":"SugarCRM, RoundCube and Request Tracker integration on a single domain","author":"Leonid Mamchenkov","date":"April 6, 2016","format":false,"excerpt":"In my years of working as a system administrator I've done some pretty complex setups and integration solutions, but I don't think I've done anything as twisted as this one recently. \u00a0The setup is part of the large and complex client project, built on their infrastructure, with quite a few\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"SAML workflow","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/04\/saml_workflow_vertical-500x469.gif?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":7599,"url":"https:\/\/mamchenkov.net\/wordpress\/2004\/06\/23\/migrating-from-mbox-to-maildir\/","url_meta":{"origin":26297,"position":1},"title":"Migrating from mbox to Maildir","author":"Leonid Mamchenkov","date":"June 23, 2004","format":false,"excerpt":"Today I had some free time on my hands, so I tried something that I was planning to do for a long time - properly move away from mbox to Maildir for email storage. It took me a bit more time than I thought it will, but I have done\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10313,"url":"https:\/\/mamchenkov.net\/wordpress\/2006\/08\/12\/i-love-gmail\/","url_meta":{"origin":26297,"position":2},"title":"I love GMail","author":"Leonid Mamchenkov","date":"August 12, 2006","format":false,"excerpt":"I have fully moved to GMail. I have yet to import my existing archives, but I lovin' it already as it is. My exim+procmail+mutt setup is great, but GMail is better for my current needs. It is, of course, less configurable, but Things That I Really Care About \u2122\u00a0(TTIRCA) do\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":16613,"url":"https:\/\/mamchenkov.net\/wordpress\/2012\/08\/11\/gmail-saga-forwarding-filters-and-pop-3\/","url_meta":{"origin":26297,"position":3},"title":"Gmail saga : forwarding, filters, and POP-3","author":"Leonid Mamchenkov","date":"August 11, 2012","format":false,"excerpt":"A few days ago I've mentioned that I have a problem with some of my email accounts. \u00a0The thing was that I have a few mailboxes, and all of them forward all incoming messages to a single account that I use for everything. \u00a0Some of the accounts forward emails using\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11510,"url":"https:\/\/mamchenkov.net\/wordpress\/2009\/02\/12\/a-better-world-with-multiple-inboxes-in-gmail\/","url_meta":{"origin":26297,"position":4},"title":"A better world with multiple inboxes in Gmail","author":"Leonid Mamchenkov","date":"February 12, 2009","format":false,"excerpt":"Google has recently added a super mega cool feature to Gmail - multiple inboxes.\u00a0 For me, it was an instant boost in productivity and huge improvement in the way I handle email. However, while I was sharing my excitement with friends and co-workers, I've received quite a few cold looks\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2009\/02\/multiple_inboxes.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":18962,"url":"https:\/\/mamchenkov.net\/wordpress\/2013\/12\/13\/safe-display-of-external-images-in-gmail\/","url_meta":{"origin":26297,"position":5},"title":"Safe display of external images in Gmail","author":"Leonid Mamchenkov","date":"December 13, 2013","format":false,"excerpt":"Official Gmail Blog lets us know that the latest update to Gmail now safely shows external images. \u00a0Most other email programs and services disable image show by default, because these can either contain all kinds of malware, or they can be used for tracking. \u00a0Gmail solves it now by downloading\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=26297"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26297\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=26297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=26297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=26297"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=26297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}