Managing Postfix Mail Aliases (On OS X Server)
Published November 18th, 2008 in Programming, Work Stuff, Computer Stuff Tags: Mac OS X Server, php, postfix.The built in Server Admin tools often aren’t enough to setup some email behaviour, that OS X Server is capable of (as part of Postfix). Postfix has a huge amount of configuration, however there’s a few things that I seem to be doing on every OS X Server installation, so I figured I’d write them down for my benefit as well as yours.
Making a no-reply blackhole address
Using your favourite text editor (in my case nano), open the following file:
sudo nano /etc/aliases
Scroll down to where it says:
# Put your local aliases here.
Be careful not to write into the bit below, as Server Admin will overwrite this to create internal mail-groups. Add the following line to create a noreply@ address on your default domain name.
noreply: /dev/null
This basically sends any mail that arrives at noreply@yourdomain.com to the /dev/null space, which is basically a pointer to the sacred unix black-hole. To apply this change, exit from the text editor, and run the command:
sudo newaliases
Piping mail to a script
If you’re a scripting/programming nut, as I am, you will appreciate the ability to directly pipe email coming into your server straight into stdin of a script. It is possible to pipe it into a old-school shell/perl script (or anything that accepts data from STDIN), however I normally like to pipe it straight into my comfort zone of PHP.
Let’s say you want to make an email address of autoreply@yourdomain.com which is handled by a PHP script. As above, use your text editor to open up /etc/aliases. Under where it says:
# Put your local aliases here.
Add the line:
autoreply: "|/path/to/scripts/pipe.php"
This pipes the RAW source of the mail to STDIN of the script specified. For a quick tip on how to receive the data from STDIN in your PHP script, here’s some code:
#!/usr/local/php5/bin/php -q
// Above line tells the shell where PHP lives on your server.
// Make sure the path is correct. Type 'whereis php' into Terminal.
// Grab the data from STDIN
$data = file_get_contents('php://stdin');
// Now do something with the $data variable
If you want to extract the different parts of the email from the raw source, I suggest you download this great MIME Parser PHP Class.
Handling ‘aliases’ on virtual mail domains
If you want to setup any of the aliases you have created above on one of your ‘virtual hosting’ domain names, you need to do one or two extra things:
Using your text editor, you need to edit the file:
sudo nano /etc/postfix/virtual
By default the file just contains a commented-out copy of the manpage. So you can type this anywhere, either above or below the existing content:
noreply@virtualdomain.com noreply
This basically makes the existing noreply@ address (as above) available under the virtual domain. I have kept the names the same to keep things simple, however there’s nothing to stop you forwarding a different name on the virtual domain to the address you have setup on your default domain.
Once you have made the relevant changes in the virtual file, you need to tell postfix’s main config where to look for it:
sudo nano /etc/postfix/main.cf
Add the following line somewhere in the file. I put it at the bottom under the rest of the config Server Admin added:
virtual_alias_maps = hash:/etc/postfix/virtual
Finally, to apply the changes run the following commands in Terminal:
sudo postfix reload
sudo postmap /etc/postfix/virtual
You can now try to send emails to these new addresses. If you get a bounce message, you’ve done something wrong!!
No Responses to “Managing Postfix Mail Aliases (On OS X Server)”