Piping Incoming Mail with PHP

PHP makes sending e-mail a relatively simple process, but receiving e-mail with PHP is less popular and certainly more complicated. There are a plethora of uses for handling incoming e-mails with PHP. In fact, a smart web developer can base an entire website off a simple e-mail handling script. This article will explore the uses of e-mail piping and will discuss how to set up an e-mail handling PHP script with CPanel or SSH on a Unix server.
E-mail piping is useful for a variety of things and has many unforeseen implementations. For example, if a PHP programmer sets up a catchall e-mail forwarder that sends all incoming e-mail to a PHP script, he can duplicate a mail service such as Mailinator (http://www.mailinator.com). In addition, an e-mail handling script could intercept text messages and then send a text message response based on the body of the text message, thus, creating a service similar to Google SMS (http://www.google.com/intl/en_us/mobile/sms/). A PHP programmer could use an e-mail handler to add e-mails requesting support to a trouble ticket database. Or, perhaps, a PHP developer could set up an online survey that users could fill out by simply replying to the e-mail. Overall, there are multiple uses for PHP e-mail handleróranging from large-scale systems to small-scale tasks.
Setting up a PHP e-mail handler in CPanel is a relatively simple task. First, any PHP handling script should begin with:

#!/usr/bin/php -q
//Listen to incoming e-mails
$sock fopen ("php://stdin"'r');
$email '';

//Read e-mail into buffer
while (!feof($sock))
$email .= fread($sock1024);

//Close socket

Note, however, that the path to PHP may vary, so the first line of the script will vary depending on the server setup. From there, it is relatively simple to parse the "to" field, the "subject" field, the "from" field, and the message from the e-mail:

//Assumes $email contains the contents of the e-mail
//When the script is done, $subject, $to, $message, and $from all contain appropriate values

//Parse "subject"
$subject1 explode ("\nSubject: "$email);
$subject2 explode ("\n"$subject1[1]);
$subject $subject2[0];

//Parse "to"
$to1 explode ("\nTo: "$email);
$to2 explode ("\n"$to1[1]);
$to str_replace ('>'''str_replace('<'''$to2[0]));

$message1 explode ("\n\n"$email);

$start count ($message1) - 3;

if (
$start 1)
$start 1;

//Parse "message"
$message2 explode ("\n\n"$message1[$start]);
$message $message2[0];

//Parse "from"
$from1 explode ("\nFrom: "$email);
$from2 explode ("\n"$from1[1]);

strpos ($from2[0], '<') !== false)
$from3 explode ('<'$from2[0]);
$from4 explode ('>'$from3[1]);
$from $from4[0];
$from $from2[0];

With those, a PHP programmer can write code to perform a variety of tasks, such as inserting the e-mail into a MySQL database or sending an e-mail reply. Once the programmer finishes the PHP script, he must tell the server to use it to handle e-mail.
Telling the server to use a PHP script to handle e-mail is relatively simple in CPanel and slightly more complicated through the command line via SSH. This article will assume that the programmer entitled the PHP script "handler.php" and uploaded it to "/home/phpshare/public_html." First, whether using CPanel or SSH, the script must be chmodded to 755. To do this via CPanel, login and click "File Manager." Then click the folder icons on the left to browse to "/home/phpshare/public_html" (or to whatever path contains the PHP script). Then click the icon next to "handler.php." In the right frame, click "Change Permissions," type "755," and click "Change." To chmod a file to 755 in SSH, type:

chmod 1755 /home/phpshare/public_html/handler.php

Next, the server must know to point to the script. To set up an e-mail pipe to a PHP script in CPanel, login and click the "Mail" icon. Then, click "Default Address" (some web hosts disallow thisótry asking them to enable it). Then click "Set Default Address" and type into the "to" box:

|php -q /home/phpshare/public_html/handler.php

Click "Change." To set up e-mail piping to a PHP script via SSH, there is a slightly more complicated process. If the server has Exim (a mail client) installed, simply follow these instructions:

First, browse to your Exim directory:
cd /etc/exim

Then create a directory to store your virtual hosts:
mkdir vhosts

Navigate to that directory:
cd vhosts

Then create a text file for your domain:
nano your_domain.com

Type in (replace "*" with an e-mail address
if you only want to handle e-mail to a certain
e-mail address with your handler):
*: |php -q /home/phpshare/public_html/handler.php

Save the file and exit.

Next, edit the configuration file for Exim
nano /usr/exim/configure

Change the domain list line to:
domainlist local_domains = @:localhost:dsearch;/etc/exim/vhosts

Type the following above "system_aliases:":
driver = redirect
domains = dsearch;/etc/exim/vhosts
data = ${expand:${lookup{$local_part}lsearch*@{/etc/exim/vhosts/$domain}}}
pipe_transport = address_pipe
file_transport = address_file

In addition, add or change "address_pipe:" to:
driver = pipe

Save the configuration file and exit.

Then restart Exim / Sendmail:
/etc/init.d/exim restart
/etc/init.d/sendmail restart

Once the server is setup to point the PHP e-mail handling script, the possibilities are endless.
In conclusion, piping e-mails to PHP scripts is useful for a variety of tasks and is simple to set up on a server running CPanel or Exim. Knowing about e-mail piping is half the battle: using it creatively is what makes it robust. PHP programmers can use e-mail piping for a variety of tasks, so do not overlook its power.




Not a member? Register today and receive the many free benefits of being a member!