Tuesday, October 14, 2008

Trigerring PHP script by mail

More and more websites are waking up to the need of making the interfaces simpler for the end user. Thanks to a trend popularized by Google, websites nowadays try and reduce the steps required to fulfill tasks online. Efforts are also made to lower the learning curve for the user.

Having said that, how would you like to let your website administrator or even the end uses to interact with your website through their daily email application? You might have already come across some websites which already allow you to do some tasks using your email. A good example is Yahoo Groups. You can read the latest messages to the group on your mail, and you can even post new messages from your email, without the need to visit the site.

I am sure that will really be useful because of the following few points:

  • User does not need to use a new way to do some task. He already knows how to send and read email
  • User does not need to remember more passwords to access the website in question. He has already authenticated if he is reading his email
  • User no longer needs to fire-up his browser, navigate to the website, login and do the task. He can do from his daily email application!
This article will show you how to do so on a classic LAMP(Linux, Apache, mySQL, PHP) configuration.

Bridging the mail and your PHP Script
The first task is to somehow be able to react in PHP to the email sent by a user. For this we first need to activate mail forwarding. I would be using Cpanel in this article to demonstrate the steps, but other web hosting management systems would have some related steps to do the same.

For the same of this article, lets say that you want to do something when your user sends a mail to subscribe@mywebsite.com. To do so follow the below steps:

  1. Login to the cpanel of your webhost for the domain on which your website would be installed. Note that this domain would be the one where your PHP is installed, and where your email account would be for your users to send mails to.
  2. In the ‘Mail’ section, click on ‘Forwarders’
  3. Click ‘Add forwarder’
  4. Under ‘Address’. Enter ‘subscribe’ in ‘Address to forward’. Your full email address would now read subscribe@mywebsite.com. Change the email address in this step to mach your requirement.
  5. In ‘Destination’, click on ‘Pipe to a program’.
  6. Enter the relative path to your script. Let’s say ‘public_html/processmail.php’.
  7. Click on ‘Add Forwarder’ button to save settings.

Processing the email contents once your script is triggered
With the mail forwarding in place, we now need to write the real script to process the mail contents. Here is a sample processmail.php script, which should be kept in the path you mentioned in the above cpanel step.



#!/usr/local/bin/php
<?php
$stdin = fopen ('php://stdin' , 'r');
while (!feof ($stdin))
{$line = trim(fgets($stdin, 4096));
$whole_mail .= "\n".$line;}
fclose ($stdin);
?>


This will read and store the complete email, with all the headers in the $whole_mail variable. Note the shebang (#!) on the top of this script. That is important, and you may need to modify it as per your host configuration.

Remember to give this script enough permissions such as perhaps 755, so that it can be executed automatically. If this script writes/creates files in some folder, then that folder will need to be given apt permissions too.

So now, with the latest email with you, you can write it to database, send additional emails from PHP or do whatever you fancy.

Note, the email received would not be stored in any mailbox, but will be discarded after the script has finished reading it.

Ofcource, the receiving email part taken care of, you can send the users back emails using the friendly ‘mail’ function of PHP. This step too can be automated to be triggered at any predefined interval using the cron jobs. This can be done in cpanel too.

This should now enable you to get great ideas on how you can add a new dimension to the user experience.

If you have more to add, or just have something to say on this article, do post a comment below. Thanks for stopping by!