Saturday, 11 June 2011

email forwarding: pipe to a program

Ever tried to read your mail outside of your mail client?
Then here's your chance to try to read or play with your incoming mails.

go to your cpanel , look for email forwarding option.
Add a new email forwarder and from the drop down list choose "pipe to a program".
In the text box enter a pipe sign `|` followed by a path to your script file.
eg.
|/home/mysite/public_html/test.php


Set it permission to 0755 and put a hash bang as the very first line of your code


In your test.php (or another) structure your code as this:


#!usr/local/bin/php -q


   --- your php code goes here---
//To read mail use this : 
$fd = fopen("php://stdin", "r"); 



#end




List of common problem one may encounter during setting up your script
1) No Input file defined?
   check the path to your script. server fail to find your file.
  set permission of your file to  0755


2)local delivery fail
    most likely reason to this problem is : file permission. change it to 755.
   if problem still persist, then try changing /usr/local/bin/php to /usr/bin/php.
   if problem still persist, then make sure that your file is not producing any output. Even a single space or line can    result in failure of execution of your script.


3) 

  1. Error in argument 1, char 2: option not found Ā
  2. Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]
  3.       php <file> [args...]

    most likely problem is path to you php interpreter is entered incorrectly.
    check it once again.
    try changing it to  /usr/local/bin/php -q if it's not already.
    if it's as such then change it to /usr/bin/php -q



4) if it still doesn't work, then i'm out of ideas. sorry. But you debug your code by writing a small hello world program first.




#!/usr/local/bin/php -q
<?php
echo "To check if this script is running or not";
mail("yourEmail@ex.com", "your subject"," Hello World");
?>


Try to test this script first. if you find your echo message in your mail, then path to your script is entered correctly.
comment that line and jump to next test.
Next is to see if you're getting  "Hello World" message in the mail body . If yes, then you're good to replace this php code with your script otherwise follow one of the above mentioned remedies.




Here's the final script i wrote to read incoming mail .



#!/usr/local/bin/php -q
<?php
$message = "got mail";
/* Read the message from STDIN */
$fd = fopen("php://stdin", "r"); 
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
$fdw = fopen("mail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);
mail('sahilsk666@gmail.com', 'My Subject', $email);
?>








No comments:

Post a Comment