January 19, 2011

Use PHP to receive email

I use "sendmail". Other MTA should be similar.

1. Supposed you want php to receive emails destined to "support@example.com". Go to /etc/aliases and add a line:
     support: "|/usr/local/bin/getemail.php"

2. restart sendmail
3. go to /etc/smrsh, and create two symbolic link files:

 ln -s /usr/bin/php php
 ln -s /usr/local/bin/getemail.php getemail.php

These symbolic links basically tells sendmail you know these programs are safe for it to run

4. create your php script. A simple php script looks like this. Of course you should add your logic to it.
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
         $email .= fread($fd, 1024);
}
fclose($fd);

file_put_contents("/tmp/email.txt",$email);
die();

No comments:

Post a Comment