Postfix Catch-all email address handled by PHP

A catch-add address accepts any emails sent to *

All incoming emails will be redirected to PHP in realtime, no POP3/IMAP and cronjob required.

1. Install postfix on server
apt-get install postfix

(Select Internet Site if prompted)

2. Point MX record to server and set exception on firewall

3. Create bot Linux user
useradd -m mailbot

4. Redirect catch-all email to the mailbot account

Add the following line to /etc/postfix/virtual (Create file if not exist, replace with actual values) mailbot

Save and close the file and run:
postmap /etc/postfix/virtual

Add the following line to /etc/postfix/
virtual_alias_maps = hash:/etc/postfix/virtual

Reload postfix service
service postfix reload


5. Create new “transport” for postfix, redirecting all email directly to stdin of PHP CLI binary

Apppend the following lines in
mailbot unix - n n - 50 pipe
flags=R user=dropbox argv=/usr/bin/php /var/www/demosite/handler.php -o SENDER=${sender} -m USER=${user} EXTENSION=${extension}

Apppend the following lines in
transport_maps = hash:/etc/postfix/transport

Apppend the following line to /etc/postfix/transport (Create file if not exist):
[email protected]  mailbot:

Rebuild the transport database
postmap /etc/postfix/transport

Reload postfix service again
service postfix reload


6. Install Mime Mail Parser and all dependencies (PEAR, MailParse)

apt-get install pear

apt-get install make

pecl install mailparse

7. Done, you can now handle everything on PHP!

Tested on Ubuntu 12.04 LTS 64bit inside Amazon EC2


Known Problem:

1. Do not use echo in PHP script, if output is too long, the thread will die and script will be interrupted.

2. UTF-8 attachment filename not decoded correctly, need to use
iconv_mime_decode($attachment->filename, 0, 'UTF-8');

3. Plain text file attachment not decoded correctly

Replacing line 400 of MimeMailParser.class.php

$write = 2028;


$write = ($len<2028)?$len:2028;


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.