{"id":574,"date":"2016-09-13T02:23:00","date_gmt":"2016-09-13T02:23:00","guid":{"rendered":"http:\/\/intelligentonlinetools.com\/blog\/?p=574"},"modified":"2018-01-08T02:55:00","modified_gmt":"2018-01-08T02:55:00","slug":"retrieving-emails-from-pop3-server-using-python-script","status":"publish","type":"post","link":"http:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/","title":{"rendered":"Retrieving Emails from POP3 Server Using Python Script"},"content":{"rendered":"<p>My inbox has a lot of data as many websites are sending notifications and updates. So I tasked myself with creating python script to extract emails from POP3 email server and organize information in better way.<br \/>\nI started from the first step &#8211; automatically reading emails from mailbox. Based on examples I have found on Internet I created the script that is retrieving emails and removing not needed information like headers.<br \/>\nI am using web based email (not gmail). In the script I am using poplib module which encapsulates a connection to a POP3 server. Another module that I am using is email &#8211; this is a library for managing email messages. As I have many emails  I limited <em>for<\/em> loop to 15 emails.<\/p>\n<p>There are still a few things that can be done. For example I would like to keep &#8220;FROM:&#8221; data, also some HTML tags still need to be removed. However this code allows to extract body text from emails and can be used as starting point.<\/p>\n<p>Feel free to provide any feedback or suggestions.<\/p>\n<p>Here is the full source code for python script to get body text emails from mailbox.<\/p>\n<pre><code>\r\n\r\nimport poplib\r\nimport email\r\n\r\n\r\nSERVER = \"server_name\"   \r\nUSER = \"email_address\"\r\nPASSWORD = \"email_password\"\r\n \r\n\r\nserver = poplib.POP3(SERVER)\r\nserver.user(USER)\r\nserver.pass_(PASSWORD)\r\n \r\n \r\nnumMessages = len(server.list()[1])\r\nif (numMessages > 15):\r\n    numMessages=15\r\nfor i in range(numMessages) :\r\n    (server_msg, body, octets) = server.retr(i+1)\r\n    for j in body:\r\n        try:\r\n            msg = email.message_from_string(j.decode(\"utf-8\"))\r\n            strtext=msg.get_payload()\r\n            print (strtext)\r\n        except:\r\n            pass\r\n\r\n<\/code><\/pre>\n<p><strong>References<\/strong><br \/>\n1. <a href=https:\/\/pythonspot.com\/en\/read-email-pop3 target=\"_blank\"> Read Email, pop3 <\/a><br \/>\n2. <a href=https:\/\/docs.python.org\/3\/library\/poplib.html target=\"_blank\"> poplib \u2014 POP3 protocol client <\/a> includes POP3 Example that opens a mailbox and retrieves and prints all messages<br \/>\n3. <a href=https:\/\/docs.python.org\/2\/library\/email.html target=\"_blank\">email \u2014 An email and MIME handling package<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>My inbox has a lot of data as many websites are sending notifications and updates. So I tasked myself with creating python script to extract emails from POP3 email server and organize information in better way. I started from the first step &#8211; automatically reading emails from mailbox. Based on examples I have found on &#8230; <a title=\"Retrieving Emails from POP3 Server Using Python Script\" class=\"read-more\" href=\"http:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[10],"tags":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Retrieving Emails from POP3 Server Using Python Script - Machine Learning Applications<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Retrieving Emails from POP3 Server Using Python Script - Machine Learning Applications\" \/>\n<meta property=\"og:description\" content=\"My inbox has a lot of data as many websites are sending notifications and updates. So I tasked myself with creating python script to extract emails from POP3 email server and organize information in better way. I started from the first step &#8211; automatically reading emails from mailbox. Based on examples I have found on ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/\" \/>\n<meta property=\"og:site_name\" content=\"Machine Learning Applications\" \/>\n<meta property=\"article:published_time\" content=\"2016-09-13T02:23:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-08T02:55:00+00:00\" \/>\n<meta name=\"author\" content=\"owygs156\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"owygs156\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/\",\"url\":\"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/\",\"name\":\"Retrieving Emails from POP3 Server Using Python Script - Machine Learning Applications\",\"isPartOf\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#website\"},\"datePublished\":\"2016-09-13T02:23:00+00:00\",\"dateModified\":\"2018-01-08T02:55:00+00:00\",\"author\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478\"},\"breadcrumb\":{\"@id\":\"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/intelligentonlinetools.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Retrieving Emails from POP3 Server Using Python Script\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#website\",\"url\":\"http:\/\/intelligentonlinetools.com\/blog\/\",\"name\":\"Machine Learning Applications\",\"description\":\"Artificial intelligence, data mining and machine learning for building web based tools and services.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/intelligentonlinetools.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478\",\"name\":\"owygs156\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"caption\":\"owygs156\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Retrieving Emails from POP3 Server Using Python Script - Machine Learning Applications","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/","og_locale":"en_US","og_type":"article","og_title":"Retrieving Emails from POP3 Server Using Python Script - Machine Learning Applications","og_description":"My inbox has a lot of data as many websites are sending notifications and updates. So I tasked myself with creating python script to extract emails from POP3 email server and organize information in better way. I started from the first step &#8211; automatically reading emails from mailbox. Based on examples I have found on ... Read more","og_url":"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/","og_site_name":"Machine Learning Applications","article_published_time":"2016-09-13T02:23:00+00:00","article_modified_time":"2018-01-08T02:55:00+00:00","author":"owygs156","twitter_card":"summary_large_image","twitter_misc":{"Written by":"owygs156","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/","url":"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/","name":"Retrieving Emails from POP3 Server Using Python Script - Machine Learning Applications","isPartOf":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/#website"},"datePublished":"2016-09-13T02:23:00+00:00","dateModified":"2018-01-08T02:55:00+00:00","author":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478"},"breadcrumb":{"@id":"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/intelligentonlinetools.com\/blog\/2016\/09\/13\/retrieving-emails-from-pop3-server-using-python-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/intelligentonlinetools.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Retrieving Emails from POP3 Server Using Python Script"}]},{"@type":"WebSite","@id":"http:\/\/intelligentonlinetools.com\/blog\/#website","url":"http:\/\/intelligentonlinetools.com\/blog\/","name":"Machine Learning Applications","description":"Artificial intelligence, data mining and machine learning for building web based tools and services.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/intelligentonlinetools.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478","name":"owygs156","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","caption":"owygs156"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7h1IJ-9g","jetpack-related-posts":[{"id":766,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/12\/31\/retrieving-post-data-using-the-wordpress-api-with-python-script\/","url_meta":{"origin":574,"position":0},"title":"Retrieving Post Data Using the WordPress API with Python Script","date":"December 31, 2016","format":false,"excerpt":"In this post we will create python script that is able to get data from WordPress (WP) blog using WP API. This script will save downloaded data into csv file for further analysis or other purposes. WP API is returning data in json format and is accessible through link http:\/\/hostname.com\/wp-json\/wp\/v2\/posts.\u2026","rel":"","context":"In &quot;API Programming&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":705,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/12\/04\/extracting-links-from-web-pages-using-different-python-modules\/","url_meta":{"origin":574,"position":1},"title":"Extracting Links from Web Pages Using Different Python Modules","date":"December 4, 2016","format":false,"excerpt":"On my previous post Web Content Extraction is Now Easier than Ever Using Python Scripting I wrote about a script, that can extract content from web page using newspaper module. Newspaper module is working well for pages that have article or newspaper format. Not all web pages have this format,\u2026","rel":"","context":"In &quot;Python Scripts&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":678,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/11\/19\/web-content-extraction-is-now-easier-than-ever-using-python-scripting\/","url_meta":{"origin":574,"position":2},"title":"Web Content Extraction is Now Easier  than Ever Using Python Scripting","date":"November 19, 2016","format":false,"excerpt":"As more and more Web content is created, there is a need for simple and efficient Web data extraction tools or scripts. With some recently released python libraries Web content extraction is now easier than ever. One example of such python library package is newspaper [1]. This module can do\u2026","rel":"","context":"In &quot;Data Mining&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1070,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/03\/12\/how-to-write-to-a-google-sheet-with-a-python-script\/","url_meta":{"origin":574,"position":3},"title":"How to Write to a Google Sheet with a Python Script","date":"March 12, 2017","format":false,"excerpt":"My post How to Write to a Google Spreadsheet with a Perl Script that was published some time ago is still getting a lot of visitors. This is not surprising as cloud computing is a fast-growing business. Below is the chart of number of searches for phrase \"Google Sheet\" from\u2026","rel":"","context":"In &quot;Python Scripts&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2017\/03\/Google-sheet-accessed-through-web-300x179.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":383,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/07\/03\/getting-the-data-from-the-web-using-php-for-api-using-the-api-with-php\/","url_meta":{"origin":574,"position":4},"title":"Getting the Data from the Web using PHP or Python for API","date":"July 3, 2016","format":false,"excerpt":"In the previous posts [1],[2] perl was used to get content from the web through Faroo API and Guardian APIs. In this post PHP and Pyhton will be used to get web data using same APIs. PHP has a powerful JSON parsing mechanism, which, because PHP is a dynamic language,\u2026","rel":"","context":"In &quot;API Programming&quot;","img":{"alt_text":"Trend for Python, Perl, PHP","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2016\/07\/trend_for_python_perl_php-300x144.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":510,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/08\/19\/getting-data-from-wikipedia-using-python\/","url_meta":{"origin":574,"position":5},"title":"Getting Data From Wikipedia Using Python","date":"August 19, 2016","format":false,"excerpt":"Recently I come across python package Wikipedia which is a Python library that makes it easy to access and parse data from Wikipedia. Using this library you can search Wikipedia, get article summaries, get data like links and images from a page, and more. Wikipedia wraps the MediaWiki API so\u2026","rel":"","context":"In &quot;API Programming&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/574"}],"collection":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/comments?post=574"}],"version-history":[{"count":10,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/574\/revisions"}],"predecessor-version":[{"id":1770,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/574\/revisions\/1770"}],"wp:attachment":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/media?parent=574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/categories?post=574"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/tags?post=574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}