{"id":3593,"date":"2017-01-27T21:30:47","date_gmt":"2017-01-27T21:30:47","guid":{"rendered":"https:\/\/islascruz.org\/blog\/?p=3593"},"modified":"2017-01-27T21:30:47","modified_gmt":"2017-01-27T21:30:47","slug":"attachments-python-imap","status":"publish","type":"post","link":"https:\/\/islascruz.org\/blog\/2017\/01\/27\/attachments-python-imap\/","title":{"rendered":"Get attachments with Python over IMAP"},"content":{"rendered":"<p>This comes from the old blog:<\/p>\n<p>I&#8217;m currently working on some stuff for my blog, well, not exactly this blog, another, I guess it may be better to sometimes post via email, sometimes just test, sometimes images. This is what I&#8217;ve got so far.<\/p>\n<pre class=\"lang:python decode:true \"># @author \u00a0 \u00a0Marco Antonio Islas Cruz &lt;markuz@islascruz.org&gt;\n# @copyright 2011 Marco Antonio Islas Cruz\n# @license \u00a0 http:\/\/www.gnu.org\/licenses\/gpl.txt\n\u00a0\nimport imaplib\nimport email\nimaplib.IMAP4.debug = imaplib.IMAP4_SSL.debug = 1\n\u00a0\nusername,passwd = ('usuario','password')\n\u00a0\ncon = imaplib.IMAP4_SSL('host',993)\ncon.login(username, passwd)\ncon.select()\ntyp, data = con.search(None, '(UNSEEN)')\nc = 0\nfor num in data[0].split():\n\u00a0 \u00a0 typ, data = con.fetch(num, '(RFC822)')\n\u00a0 \u00a0 c +=1\n\u00a0 \u00a0 text = data[0][1]\n\u00a0 \u00a0 msg = email.message_from_string(text)\n\u00a0 \u00a0 for part in msg.walk():\n\u00a0 \u00a0 \u00a0 \u00a0 if part.get_content_maintype() == 'multipart':\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 continue\n\u00a0 \u00a0 \u00a0 \u00a0 if part.get('Content-Disposition') is None:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 continue\n\u00a0 \u00a0 \u00a0 \u00a0 filename = part.get_filename()\n\u00a0 \u00a0 \u00a0 \u00a0 data = part.get_payload(decode=True)\n\u00a0 \u00a0 \u00a0 \u00a0 if not data:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 continue\n\u00a0 \u00a0 \u00a0 \u00a0 f \u00a0= open(os.join(os.environ['HOME'],filename), 'w')\n\u00a0 \u00a0 \u00a0 \u00a0 f.write(data)\n\u00a0 \u00a0 \u00a0 \u00a0 f.close()\n\u00a0 \u00a0 \u00a0 \u00a0 \ncon.close()\ncon.logout()\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The code is available under the GPL license. You can follow this script (if it ever evolves) in <a href=\"https:\/\/github.com\/markuz\/scripts\/blob\/master\/getmail.py\" target=\"_blank\" rel=\"noopener\">github<\/a>.<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_3593\" class=\"pvc_stats all  \" data-element-id=\"3593\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/islascruz.org\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This comes from the old blog: I&#8217;m currently working on some stuff for my blog, well, not exactly this blog, another, I guess it may be better to sometimes post via email, sometimes just test, sometimes images. This is what I&#8217;ve got so far. # @author \u00a0 \u00a0Marco Antonio Islas Cruz &lt;markuz@islascruz.org&gt; # @copyright 2011 [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_3593\" class=\"pvc_stats all  \" data-element-id=\"3593\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/islascruz.org\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":6300,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-personal"],"a3_pvc":{"activated":true,"total_views":22594,"today_views":0},"brizy_media":[],"_links":{"self":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/3593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/comments?post=3593"}],"version-history":[{"count":2,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/3593\/revisions"}],"predecessor-version":[{"id":3603,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/posts\/3593\/revisions\/3603"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/media\/6300"}],"wp:attachment":[{"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/media?parent=3593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/categories?post=3593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/islascruz.org\/blog\/wp-json\/wp\/v2\/tags?post=3593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}