<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>DAN FOLKES &#187; pop</title>
	<atom:link href="http://danfolkes.com/index.php/tag/pop/feed/" rel="self" type="application/rss+xml" />
	<link>http://danfolkes.com</link>
	<description>programmers blog</description>
	<lastBuildDate>Fri, 03 Feb 2012 15:58:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<atom:link rel="next" href="http://danfolkes.com/index.php/tag/pop/feed/?page=2" />

		<item>
		<title>Python: Grab Email from Gmail and Insert into MySql Database</title>
		<link>http://danfolkes.com/2009/01/22/python-grab-email-from-gmail-and-insert-into-mysql-database/</link>
		<comments>http://danfolkes.com/2009/01/22/python-grab-email-from-gmail-and-insert-into-mysql-database/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 17:21:31 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[pop]]></category>

		<guid isPermaLink="false">http://danfolkes.com/?p=113</guid>
		<description><![CDATA[This script will: Log into Gmail Pop Read the email Delete the read email Insert the email&#8217;s text into a MySql database Sleep for 1800 seconds, and repeat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [...]


Related posts:<ol><li><a href='http://danfolkes.com/2008/09/30/python-wunderground-todays-weather-to-email-sms-to-phone/' rel='bookmark' title='Permanent Link: Python: Wunderground Todays Weather to Email SMS to Phone'>Python: Wunderground Todays Weather to Email SMS to Phone</a></li>
<li><a href='http://danfolkes.com/2010/03/31/php-gmail-imap-connection-with-database-insert/' rel='bookmark' title='Permanent Link: PHP Gmail Imap Connection with Database Insert'>PHP Gmail Imap Connection with Database Insert</a></li>
<li><a href='http://danfolkes.com/2008/02/06/torrent-search/' rel='bookmark' title='Permanent Link: Python Torrent Search and Download (TPB)'>Python Torrent Search and Download (TPB)</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>This script will:</p>
<ul>
<li>Log into Gmail Pop</li>
<li>Read the email</li>
<li>Delete the read email</li>
<li>Insert the email&#8217;s text into a MySql database</li>
<li>Sleep for 1800 seconds, and repeat</li>
</ul>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">poplib</span>, <span style="color: #dc143c;">sys</span>, <span style="color: #dc143c;">time</span>
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">string</span>, <span style="color: #dc143c;">random</span>
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">StringIO</span>, <span style="color: #dc143c;">rfc822</span>
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span>
        SERVER = <span style="color: #483d8b;">&quot;pop.gmail.com&quot;</span>
        USER  = <span style="color: #483d8b;">&quot;gmailusername&quot;</span>
        PASSWORD = <span style="color: #483d8b;">&quot;gmailpassword&quot;</span>
        i = <span style="color: #ff4500;">0</span><span style="color: #66cc66;">;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;&quot;&quot;
        |------------------------------------------|
        |  This is a python program that checks a  |
        |  POP account and if there is a message,  |
        |  it adds it to the SQL server.           |
        |------------------------------------------|
                  by: Daniel Folkes
                         email: danfolkes@gmail.com
&nbsp;
        (every 180 seconds)
        Checking POP server....
&quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff4500;">1</span>:
                <span style="color: #ff7700;font-weight:bold;">try</span>:
                        server = <span style="color: #dc143c;">poplib</span>.<span style="color: black;">POP3_SSL</span><span style="color: black;">&#40;</span>SERVER, <span style="color: #ff4500;">995</span><span style="color: black;">&#41;</span>
                        server.<span style="color: #dc143c;">user</span><span style="color: black;">&#40;</span>USER<span style="color: black;">&#41;</span>
                        server.<span style="color: black;">pass_</span><span style="color: black;">&#40;</span>PASSWORD<span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">except</span>:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;error setting up server.&quot;</span>
&nbsp;
&nbsp;
                resp, items, octets = server.<span style="color: #008000;">list</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                <span style="color: #808080; font-style: italic;"># download a random message</span>
                <span style="color: #ff7700;font-weight:bold;">try</span>:
                        <span style="color: #008000;">id</span>, size = <span style="color: #dc143c;">string</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span>items<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                        resp, text, octets = server.<span style="color: black;">retr</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span><span style="color: black;">&#41;</span>
&nbsp;
                        text = <span style="color: #dc143c;">string</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>text, <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
                        <span style="color: #008000;">file</span> = <span style="color: #dc143c;">StringIO</span>.<span style="color: #dc143c;">StringIO</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
                        note = <span style="color: #483d8b;">&quot;&quot;</span>
                        name = <span style="color: #483d8b;">&quot;&quot;</span>
                        message = <span style="color: #dc143c;">rfc822</span>.<span style="color: black;">Message</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span>
                        <span style="color: #ff7700;font-weight:bold;">for</span> k, v <span style="color: #ff7700;font-weight:bold;">in</span> message.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
                                <span style="color: #ff7700;font-weight:bold;">if</span> k==<span style="color: #483d8b;">'from'</span>:
                                                name = v<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">12</span><span style="color: black;">&#93;</span>
                        note = message.<span style="color: black;">fp</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>:<span style="color: #ff4500;">50</span><span style="color: black;">&#93;</span>
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;note: &quot;</span>, note
                        server.<span style="color: black;">dele</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#this will delete the message after you read it</span>
                        server.<span style="color: black;">quit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                <span style="color: #808080; font-style: italic;">#-------------------------------------------</span>
                        <span style="color: #ff7700;font-weight:bold;">if</span> note <span style="color: #66cc66;">!</span>=<span style="color: #483d8b;">&quot;&quot;</span>:
                                <span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb
                                db = MySQLdb.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>host=<span style="color: #483d8b;">&quot;localhost&quot;</span>, <span style="color: #dc143c;">user</span>=<span style="color: #483d8b;">&quot;USERNAME&quot;</span>, passwd=<span style="color: #483d8b;">&quot;PASSWORD&quot;</span>,db=<span style="color: #483d8b;">&quot;DATABASENAME&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
                                cur2 = db.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                                <span style="color: #ff7700;font-weight:bold;">if</span> name:
                                        cur2.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;INSERT INTO note (note, name) VALUES (%s, %s)&quot;</span>, <span style="color: black;">&#40;</span>note, name<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                                <span style="color: #ff7700;font-weight:bold;">else</span>:
                                        cur2.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;INSERT INTO note (note) VALUES (%s)&quot;</span>, <span style="color: black;">&#40;</span>note<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">except</span>:
                        i+=<span style="color: #ff4500;">1</span>
                        <span style="color: #808080; font-style: italic;">#print &quot;Unexpected error:&quot;, sys.exc_info()[0]</span>
                        <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1800</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">except</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Failed Unexpectedly&quot;</span></pre></td></tr></table></div>



<p>Related posts:<ol><li><a href='http://danfolkes.com/2008/09/30/python-wunderground-todays-weather-to-email-sms-to-phone/' rel='bookmark' title='Permanent Link: Python: Wunderground Todays Weather to Email SMS to Phone'>Python: Wunderground Todays Weather to Email SMS to Phone</a></li>
<li><a href='http://danfolkes.com/2010/03/31/php-gmail-imap-connection-with-database-insert/' rel='bookmark' title='Permanent Link: PHP Gmail Imap Connection with Database Insert'>PHP Gmail Imap Connection with Database Insert</a></li>
<li><a href='http://danfolkes.com/2008/02/06/torrent-search/' rel='bookmark' title='Permanent Link: Python Torrent Search and Download (TPB)'>Python Torrent Search and Download (TPB)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://danfolkes.com/2009/01/22/python-grab-email-from-gmail-and-insert-into-mysql-database/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
	</item>
	</channel>
</rss>

