Postfix Catch-all email address handled by PHP

A catch-add address accepts any emails sent to *@mydomain.com.

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 yourdomain.com with actual values)
@yourdomain.com mailbot

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

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

Reload postfix service
service postfix reload

Source: http://www.cyberciti.biz/faq/howto-setup-postfix-catch-all-email-accounts/

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

Apppend the following lines in master.cf:
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 main.cf:
transport_maps = hash:/etc/postfix/transport

Apppend the following line to /etc/postfix/transport (Create file if not exist):
mailbot@mydomain.com  mailbot:

Rebuild the transport database
postmap /etc/postfix/transport

Reload postfix service again
service postfix reload

Source: http://www.refactor.co.za/2012/12/25/emailing-attachments-directly-to-a-dropbox-folder/

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;

with

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

Source: http://code.google.com/p/php-mime-mail-parser/issues/detail?id=7#c7

Leave a Reply

Your email address will not be published.