<?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>Stephan&#039;s Blog &#187; Postfix</title>
	<atom:link href="http://www.arlt.eu/blog/tag/postfix/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.arlt.eu/blog</link>
	<description>Life in Technicolor.</description>
	<lastBuildDate>Tue, 01 Dec 2009 00:00:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Postfix and MySQL as a Mail Forwarding Service</title>
		<link>http://www.arlt.eu/blog/2009/11/19/postfix-and-mysql-as-a-mail-forwarding-service/</link>
		<comments>http://www.arlt.eu/blog/2009/11/19/postfix-and-mysql-as-a-mail-forwarding-service/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 00:00:00 +0000</pubDate>
		<dc:creator>Stephan</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Mail]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Postfix]]></category>

		<guid isPermaLink="false">http://www.arlt.eu/blog/?p=306</guid>
		<description><![CDATA[The Postfix mail transfer agent (MTA) provides optional lookup tables that alias specific mail addresses or domains to other local or remote addresses. In the main.cf configuration file of Postfix you can define a lookup table (a mapping file) by using the virtual_alias_maps keyword:

1
virtual_alias_maps = hash:/etc/postfix/virtual

In the assigned mapping file (e.g. virtual) there is now [...]]]></description>
			<content:encoded><![CDATA[<p>The Postfix mail transfer agent (MTA) provides optional lookup tables that alias specific mail addresses or domains to other local or remote addresses. In the <code>main.cf</code> configuration file of Postfix you can define a lookup table (a mapping file) by using the <code>virtual_alias_maps</code> keyword:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="text" style="font-family:monospace;">virtual_alias_maps = hash:/etc/postfix/virtual</pre></td></tr></table></div>

<p>In the assigned mapping file (e.g. <code>virtual</code>) there is now space to set up the forwarding of a mail address (pattern) to another mail address (result):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="text" style="font-family:monospace;">info@example.com webmaster@example.com
mail@example.com webmaster@example.com</pre></td></tr></table></div>

<p>To enable this configuration you have to execute <code>postmap virtual</code> and <code>postfix reload</code>. As you can see here, every time the mapping has changed, somebody must create the hash table and reload the entire MTA using these commands. To avoid this, Postfix supports MySQL mapping. Here the forwarding configuration is not coming from a mapping file anymore but from a MySQL table. Then you&#8217;ll be able to dynamically add, edit or remove forwardings without calling the commands above. In Debian / Ubuntu the <code>postfix-mysql</code> package is required. In the <code>main.cf</code> configuration file the definition of the lookup table (mapping file) changes as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="text" style="font-family:monospace;">virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf</pre></td></tr></table></div>

<p>In the assigned mapping file (e.g. <code>mysql-virtual-alias-maps.cf</code>) there is now space to set up the bridge for connecting Postfix and MySQL (using the well-known parameters plus a query):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="text" style="font-family:monospace;">user = myuser
password = mypassword
hosts = 127.0.0.1
dbname = mydb
query = SELECT result FROM forwardings WHERE pattern = '%s'</pre></td></tr></table></div>

<p>Last but not least just create the <code>forwardings</code> table in your MySQL database. Adding rows to that table is like adding forwardings in the mapping file above. And again, no reload etc. is required because Postfix queries the forwardings on demand.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> forwardings <span style="color: #66cc66;">&#40;</span>
    pattern VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
    result VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> forwardings <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'info@example.com'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'webmaster@example.com'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> forwardings <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'mail@example.com'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'webmaster@example.com'</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.arlt.eu/blog/2009/11/19/postfix-and-mysql-as-a-mail-forwarding-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
