Dreamhost and procmail – parsing your mail with a script

I have a Dreamhost account and have done for some while. Typically I’m happy with their service, but yesterday was the first real frustration I had with them. I wanted to use procmail to send email to a script for processing. I was sure I had done this before on Dreamhost, but couldn’t seem to get it to work in the wee hours of the night. So after a quick email to support asking if it was possible, they came back with this response:

Unfortunately this is not possible. We do
not offer or support any type of email to script functionality on our
servers.

And this pretty much backed up all the searches I had been doing on line in regards to Dreamhost and using procmail.

Now, where they may not support people doing this, it is my pleasure to tell you that it is possible! And here’s how you do it…

The first think you have to do is set up the email address that you want to have processed – this can be done from the control panel under ‘Mail > Manage email > Create new email address‘. Let’s pretend that you set one up called process@yourdomain.com.

Now you have your email user set up you need to create a shell account user – one that can log in via SSH. This can be done from ‘Users > Manage users > Add a new user‘. It’s important that you select the ‘Shell: allows SFTP/FTP plus shell access’ option. (You may want to use one of your existing shell users, of course, but I personally set one up just for the purpose as Dreamhost doesn’t limit you on the number of users you can have.)

Now that you have the email address and shell user account set up, you now need to forward whatever goes to that address to the shell account. This is done with a filter, which can be modified in ‘Mail > Message filters‘. You can have multiple filters on the same address, so you may do some additional processing, but the essential filter to have for the script parsing is to have the email sent to the shell user. So enter your filter string (I made mine so that the ‘To’ contains ‘*’ – ie, everything, but you may only want it matching something particular for processing by the script), and select the ‘Forward to shell account’ option with the appropriate user selected. Then select the option what to do when the filter matches – ‘execute and stop’ or ‘execute and continue’. I chose the former because I had only the one filter, but you will want it to continue if you have more (or stop is this is the last of multiple filters).

OK, we’re about half way there…

Now you want to log in to the shell account and create a few files. The first file has the filename .forward.postfix and has the contents:

"|/usr/bin/procmail -t"

(Yes, you need the quotes!)

The next file is your procmail rule, and that’s in a file that’s called .procmailrc. It’s possible to do a lot of things with procmail and I’m going to explain none of them here! At the end of this post will be some links about procmail for you to check out and learn from. Instead, I’ll just show you what I have and you can go from there.

DEFAULT=$HOME/Maildir/
MAILDIR=$HOME/Maildir
PMDIR=$HOME/.procmail
LOGFILE=$PMDIR/log.`date +%y-%m-%d`
SHELL=/bin/sh

:0
|`/usr/local/php53/bin/php /home/youruser/yourscript.php`

The first four lines just set up various things; the location of your mail directory, the log file name, and so on. If the directory .procmail doesn’t exist in your root directory then you may want to create it.

The last two lines are the procmail recipe. Often here you will see three lines – the :0 to start a rule (sometimes with a lock file – again, follow the procmail links for info) then a filter, such a From.*person@domain and then the ‘what to do if this matches’ part. I skipped the filter because I wanted to capture anything that came in to my email address.

The next line is really the important one. You need the pipe (the ‘|’) to pipe the content of the email to a script execute via a shell command. And that’s crucial here – you need to wrap your command up in backticks (`) for it to work – I had no luck without those.

You’ll notice here that I’m using the PHP binary specifically at /usr/local/php53/bin/php – this is because I wanted to use PHP 5.3.

And there you have it – send an email to process@yourdomain.com and the script (in the example above, yourscript.php) will be able to read in the email via stdin and do something with it.

By the way, the PHP code to get the email in to a variable is:

$rawEmail = '';
if (($fp = fopen('php://stdin', 'r')) !== false) {
    while (!feof($fp)) {
        $rawEmail .= fread($fp, 1024);
    }
    fclose($fp);
}

You can then parse it for multiparts and so on. If you’re like me and want a really easy way to do that, just use Zend_Mail_Message:

$email = new Zend_Mail_Message(array(
    'raw' => $rawEmail
));

Procmail links

All of these are external links and I cannot vouch for the quality of any of them or how much you’ll learn from reading them. However, as one if procmail’s very own homepage, you can’t really go wrong!

Did you like this? Share it:

Leave a Reply