{"id":391,"date":"2014-04-22T12:43:48","date_gmt":"2014-04-22T16:43:48","guid":{"rendered":"http:\/\/minireference.com\/blog\/?p=391"},"modified":"2014-04-22T12:43:48","modified_gmt":"2014-04-22T16:43:48","slug":"ghetto-crm","status":"publish","type":"post","link":"https:\/\/minireference.com\/blog\/ghetto-crm\/","title":{"rendered":"Ghetto CRM"},"content":{"rendered":"<p>Say you want to extract the names and emails from all the messages under given tag in your gmail. In my case, it&#8217;s the 60 readers who took part in the &#8220;free PDF if you buy the print version&#8221; offer. I&#8217;d like to send them an update.<\/p>\n<p>I started clicking around in gmail and compiling the list, but Gmail&#8217;s UI is NOT designed for this, you can&#8217;t select-text the email field because a popup shows up, and yada yada&#8230;. If you&#8217;re reading this, you probably got to this post because you have the same problem so I don&#8217;t need to explain.<\/p>\n<p>Yes this is horribly repetitive, and yes it can be <strong>automated using python<\/strong>:<\/p>\n<pre>\nimport imaplib\nimport email\nfrom email.utils import parseaddr\nimport getpass\n\n\nuser = raw_input(\"Enter your GMail username:\")\npwd = getpass.getpass(\"Enter your password: \")\n\nm = imaplib.IMAP4_SSL('imap.gmail.com', 993)    \nm.login(user,pwd)    \n\n# see IMAP client\n# m\n# see tags (i.e. mailboxes) using\n# m.list()\n\n\n# select the desired tag\nm.select('miniref\/lulureaders', readonly=True)\ntyp, data = m.search(None, 'ALL')\n\n\n# build a list of people from (both FROM and TO headers)\npeople = []\nfor i in range(1, len(data[0].split(' '))+1 ):\n    typ, msg_data = m.fetch(str(i), '(RFC822)')\n    for response_part in msg_data:\n        if isinstance(response_part, tuple):\n            msg = email.message_from_string(response_part[1])\n            name1, addr1 = parseaddr( msg['to'] )\n            name2, addr2 = parseaddr( msg['from'] )\n            d1 = { \"name\":name1, \"email\":addr1 }\n            d2 = { \"name\":name2, \"email\":addr2 }\n            people.extend([d1,d2])\n            # uncomment below to see wat-a-gwaan-on \n            #for header in [ 'subject', 'to', 'from' ]:\n            #    print '%-8s: %s' % (header.upper(), msg[header])\n            #print \"-\"*70\n\n# lots of people, duplicate entries\nlen(people)\n\n# filter uniq\n# awesome trick by gnibbler \n# via http:\/\/stackoverflow.com\/questions\/11092511\/python-list-of-unique-dictionaries\npeople =  {d['email']:d for d in people}.values()     # uniq by email\n\n# just uniques\nlen(people)\n\n# print as comma separated values for import into mailing list\nfor reader in people:\n    print reader['email'] + \", \" + reader['name']\n    \n# ciao!\nm.close()\n\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Say you want to extract the names and emails from all the messages under given tag in your gmail. In my case, it&#8217;s the 60 readers who took part in the &#8220;free PDF if you buy the print version&#8221; offer. I&#8217;d like to send them an update. I started clicking around in gmail and compiling [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,15],"tags":[],"class_list":["post-391","post","type-post","status-publish","format-standard","hentry","category-marketing","category-tools"],"_links":{"self":[{"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/posts\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/comments?post=391"}],"version-history":[{"count":0,"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/posts\/391\/revisions"}],"wp:attachment":[{"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/media?parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/categories?post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/tags?post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}