<?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>bitwalker.nl &#187; python</title>
	<atom:link href="http://www.bitwalker.nl/blog/category/python/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bitwalker.nl</link>
	<description>agile software development</description>
	<lastBuildDate>Sun, 15 Aug 2010 20:49:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Filtering mail attachments with maildrop and python</title>
		<link>http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python</link>
		<comments>http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python#comments</comments>
		<pubDate>Fri, 14 Nov 2008 12:55:27 +0000</pubDate>
		<dc:creator>Harald Walker</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[maildrop]]></category>

		<guid isPermaLink="false">http://www.bitwalker.nl/?p=127</guid>
		<description><![CDATA[For a new feature of our application we had to filter incoming emails based on header values in attached emails. As I could not find examples on how to filter on attachments directly with maildropfilter, I decided to handle the attachments with an external script. In the solution described below, the script returns a value [...]]]></description>
			<content:encoded><![CDATA[<p>For a new feature of our application we had to filter incoming emails based on header values in attached emails. As I could not find examples on how to filter on attachments directly with maildropfilter, I decided to handle the attachments with an external script. In the solution described below, the script returns a value but instead it could also modify the incoming mail and then one would use the <em>xfilter</em> instruction of <a href="http://www.courier-mta.org/maildropfilter.html">maildropfilter</a>.<span id="more-127"></span></p>
<p>The python script reads the email from stdin using the <a href="http://www.python.org/doc/2.5.2/lib/module-email.html">email package</a> and then iterates over all the parts and subparts of the message object tree. In our example we are looking for a header called &#8216;x-mailer&#8217; and if the value is BigMailer, we return a value to the maildrop filter.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">email</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
msg = <span style="color: #dc143c;">email</span>.<span style="color: black;">message_from_file</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">stdin</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> part <span style="color: #ff7700;font-weight:bold;">in</span> msg.<span style="color: black;">walk</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'x-mailer'</span> <span style="color: #ff7700;font-weight:bold;">in</span> part:
                <span style="color: #ff7700;font-weight:bold;">if</span> part<span style="color: black;">&#91;</span><span style="color: #483d8b;">'x-mailer'</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'BigMailer'</span>:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;BigMailer&quot;</span>
                        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span></pre></div></div>

<p>From <em>maildroprc</em> the python script is being executed when there is a match for a certain subject and in case the script returns &#8216;BigMailer&#8217;, the email is being delivered to the <em>mybigmailer</em> mailbox.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">/</span>^Subject:.<span style="color: #000000; font-weight: bold;">*</span>Mail with attachment mail<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #007800;">SOURCE</span>=<span style="color: #000000; font-weight: bold;">`/</span>opt<span style="color: #000000; font-weight: bold;">/</span>csw<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>csw<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>maildrop<span style="color: #000000; font-weight: bold;">/</span>myfilter.py<span style="color: #000000; font-weight: bold;">`</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$SOURCE</span> eq <span style="color: #ff0000;">&quot;BigMailer&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #7a0874; font-weight: bold;">&#123;</span>
     to <span style="color: #000000; font-weight: bold;">/</span>vmail<span style="color: #000000; font-weight: bold;">/</span>mybigmailer<span style="color: #000000; font-weight: bold;">/</span>
     <span style="color: #7a0874; font-weight: bold;">exit</span>
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.bloglines.com/sub/http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python" rel="nofollow" title="Add to&nbsp;Bloglines"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/bloglines.png" title="Add to&nbsp;Bloglines" alt="Add to&nbsp;Bloglines" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://blogmarks.net/my/new.php?mini=1&amp;simple=1&amp;url=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;title=Filtering+mail+attachments+with+maildrop+and+python" rel="nofollow" title="Add to&nbsp;Blogmarks"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/bmarks.png" title="Add to&nbsp;Blogmarks" alt="Add to&nbsp;Blogmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;title=Filtering+mail+attachments+with+maildrop+and+python" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;title=Filtering+mail+attachments+with+maildrop+and+python" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Filtering+mail+attachments+with+maildrop+and+python&amp;url=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;title=Filtering+mail+attachments+with+maildrop+and+python" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://fleck.com/litebookmarklet.php?url=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;title=Filtering+mail+attachments+with+maildrop+and+python" rel="nofollow" title="Add to&nbsp;Fleck"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/fleck.png" title="Add to&nbsp;Fleck" alt="Add to&nbsp;Fleck" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;title=Filtering+mail+attachments+with+maildrop+and+python" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://netvouz.com/action/submitBookmark?url=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;title=Filtering+mail+attachments+with+maildrop+and+python&amp;popup=no" rel="nofollow" title="Add to&nbsp;Netvouz"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/netvouz.png" title="Add to&nbsp;Netvouz" alt="Add to&nbsp;Netvouz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;title=Filtering+mail+attachments+with+maildrop+and+python" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;title=Filtering+mail+attachments+with+maildrop+and+python" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;title=Filtering+mail+attachments+with+maildrop+and+python" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;title=Filtering+mail+attachments+with+maildrop+and+python" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python&amp;t=Filtering+mail+attachments+with+maildrop+and+python" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://www.bitwalker.nl/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.bitwalker.nl/blog/filtering-mail-attachments-with-maildrop-and-python/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
