<?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/"
	>

<channel>
	<title>insanesecurity &#187; Python</title>
	<atom:link href="http://insanesecurity.info/blog/tag/python/feed" rel="self" type="application/rss+xml" />
	<link>http://insanesecurity.info/blog</link>
	<description>security through a distorted eye</description>
	<lastBuildDate>Thu, 25 Feb 2010 22:31:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>email backup with Python</title>
		<link>http://insanesecurity.info/blog/email-backup-with-python</link>
		<comments>http://insanesecurity.info/blog/email-backup-with-python#comments</comments>
		<pubDate>Thu, 02 Jul 2009 11:12:37 +0000</pubDate>
		<dc:creator>dblackshell</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://insanesecurity.info/blog/?p=99</guid>
		<description><![CDATA[In my new project (involving spam) I&#8217;ve created a script for retrieving the messages locally, which you could also use for rapidly backing up your emails. The script is aimed for retrieving the emails for multiple accounts using the POP3 protocol (via SSL). #!/usr/bin/env python import poplib, os users = { "username1":"password1", "username2":"password2" } mail_server [...]]]></description>
			<content:encoded><![CDATA[<p>In my new project (involving spam) I&#8217;ve created a script for retrieving the messages locally, which you could also use for rapidly backing up your emails.</p>
<p>The script is aimed for retrieving the emails for multiple accounts using the POP3 protocol (via SSL).<br />
<span id="more-99"></span></p>
<pre>
#!/usr/bin/env python

import poplib, os

users = {
    "username1":"password1",
    "username2":"password2"
}

mail_server = 'mail.hosting.com'

for user in users:
    m = poplib.POP3_SSL(mail_server)
    m.user(user)
    m.pass_(users[user])
    num = len(m.list()[1])
    for i in range(1, num+1):
        uid  = m.retr(i)[2]
        mail = m.retr(i)[1]
        if not os.access(user, os.F_OK):
            os.mkdir(user, 0777)
        mfile  = user+'/'+str(uid)+'.txt'
        mstore = open(mfile, 'w')
        mstore.write("\n".join(mail))
        mstore.close()
    m.quit()
</pre>
<p>Of course you should replace <code>poplib.POP3_SSL</code> with <code>poplib.POP3</code> if SSL is not supported by your mail server.</p>
<p>Hope you find it useful&#8230; going back to work.</p>
]]></content:encoded>
			<wfw:commentRss>http://insanesecurity.info/blog/email-backup-with-python/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
