Piping Incoming Mail with PHP | PHP Share

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:

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:

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

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:mkdirvhosts

Navigate to that directory:cdvhosts

Then create a text file for your domain:nanoyour_domain.com

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

Save the file and exit.

Next, edit the configuration file for Eximnano/usr/exim/configure

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

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.