{"id":239,"date":"2023-02-23T15:02:54","date_gmt":"2023-02-23T20:02:54","guid":{"rendered":"https:\/\/www.davidhedges.info\/?p=239"},"modified":"2023-02-23T15:02:54","modified_gmt":"2023-02-23T20:02:54","slug":"using-python-download-mail-attachments","status":"publish","type":"post","link":"https:\/\/www.davidhedges.info\/index.php\/2023\/02\/23\/using-python-download-mail-attachments\/","title":{"rendered":"Using Python Download Mail Attachments"},"content":{"rendered":"\n<p>We had been using an online fax that sends faxes as a pdf attachment to email. We didn&#8217;t want to manage the email account, but instead wanted to create a script to strip the attachments and then convert them to compressed tiff format. <\/p>\n\n\n\n<p>You&#8217;ll need python and imagemagick installed to make it all work. in python you&#8217;ll also need to install the poplib and email modules. you can install these using pip:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install poplib\npip install email<\/code><\/pre>\n\n\n\n<p>if you are using gmail or mail hosted off of google, you&#8217;ll need to change your security settings. First, click your account in the upper right, then choose &#8220;manage your google account&#8221;. Select security from the menu on the left, then scroll down looking for &#8220;less secure app access&#8221;. Choose Less Secure app access, and turn it on. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"856\" height=\"220\" src=\"https:\/\/www.davidhedges.info\/wp-content\/uploads\/2023\/02\/image.png\" alt=\"\" class=\"wp-image-240\" srcset=\"https:\/\/www.davidhedges.info\/wp-content\/uploads\/2023\/02\/image.png 856w, https:\/\/www.davidhedges.info\/wp-content\/uploads\/2023\/02\/image-300x77.png 300w, https:\/\/www.davidhedges.info\/wp-content\/uploads\/2023\/02\/image-768x197.png 768w\" sizes=\"auto, (max-width: 856px) 100vw, 856px\" \/><\/figure>\n\n\n\n<p>the following is the python code I had put together to get this to work.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python3\r\nimport poplib\r\nimport email\r\nimport os\r\nimport time\r\nfrom subprocess import Popen, PIPE\r\n'''\r\nPython script to pull pdf atachments from email\r\nWritten by: David Hedges:: dhedges at hedgesadvantage dot com\r\n'''\r\n\r\nsavedir = \"&lt;path to save location>\"\r\n\r\nserver = poplib.POP3_SSL('pop.gmail.com', '995')\r\nserver.user('&lt;email login')\r\nserver.pass_('&lt;email password>')\r\n\r\n\r\nemails, tbytes = server.stat()\r\nmsg_list = server.list()\r\n\r\n\r\ndef pdf2tiff(dpath,name):\r\n    '''convert the pdf to tiff'''\r\n    newf = name.replace('.pdf', '.tif')\r\n    os.chdir(dpath)\r\n    runner = Popen(&#91;'convert -density 200x100 -compress zip -depth 1 '+name+' '+newf], shell=True, stdout=PIPE).wait()\r\n    '''fix perms'''\r\n    os.chmod(dpath+'\/'+name, 0o777)\r\n    os.chmod(dpath+'\/'+newf, 0o777)\r\n    '''remove pdf'''\r\n    os.remove(dpath+'\/'+name)\r\n\r\n\r\nfor i in range(emails):\r\n    msg_data = server.retr(i+1)\r\n    lines = msg_data&#91;1]\r\n    str_message = email.message_from_bytes(b'\\n'.join(lines))\r\n    #print(str_message)\r\n    for part in str_message.walk():\r\n        print(part.get_content_type())\r\n        if part.get_content_type() == \"application\/pdf\":\r\n            filename = part.get_filename()\r\n            if not(filename):\r\n                filename = \"test.pdf\"\r\n            #print(filename)\r\n            tvar = time.strftime(\"%H%M%S\")\r\n            fname = filename.replace('.pdf', tvar+'.pdf')\r\n            fp = open(os.path.join(savedir, fname), 'wb')\r\n            fp.write(part.get_payload(decode=1))\r\n            fp.close\r\n            pdf2tiff(savedir, fname)\r\n            server.dele(i+1)\r\n\r\nserver.quit()\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We had been using an online fax that sends faxes as a pdf attachment to email. We didn&#8217;t want to manage the email account, but instead wanted to create a script to strip the attachments and then convert them to compressed tiff format. You&#8217;ll need python and imagemagick installed to make it all work. in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[3],"tags":[],"class_list":{"0":"post-239","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-it-blog","7":"entry"},"_links":{"self":[{"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/posts\/239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/comments?post=239"}],"version-history":[{"count":1,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/posts\/239\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/posts\/239\/revisions\/241"}],"wp:attachment":[{"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/media?parent=239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/categories?post=239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.davidhedges.info\/index.php\/wp-json\/wp\/v2\/tags?post=239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}