Russ Posted November 18, 2009 Share Posted November 18, 2009 I am trying to write a php script that will read and process an email. I want to pipe an email to that script. I am loading the script to my cgi folder and giving it permissions of 755. I am then setting up an email forwarder in cPanel and selecting the "Pipe to Program" option. That option includes the following instructions: When piping to a program, you should enter a path relative to your home directory. If the script requires an interpreter such as perl or php, you should omit the "/usr/bin/perl, or /usr/bin/php portion." Make sure that your script is executable and has the appropriate Hashbang at the top of the script. If you do not know how to add the Hashbang, just make sure to name your script file with the correct extension and you will be prompted to have the Hashbang added automatically. As instructed, I inserted path to my script, which is: /home/*******/cgi-bin/pipemail.php Note, the red text is added automatically, and I need only input the balance of the path. My script is not working. When I send a test email to the address that is forwarded to the script, I get a bounce back email that states, in part: A message that you sent could not be delivered to one or more of its recipients. This is a permanent error. The following address(es) failed: pipe to |/home/******/cgi-bin/pipemail.php generated by testphp@mydomain.net local delivery failed I am trying to figure out where I am going wrong. 1. Assuming that the script is properly written (from a syntax and logic standpoint), am I setting up this properly on my site? As far as uploading the script to my cgi folder? Also tried it in my scgi folder and in a "test" folder created in my /public_html folder. Neither of these of those work. 2. I am not sure what to use for the "Hashbang" and contrary to the instructions quoted above, when I set up the forwarder, I was not "prompted to have the Hashbang added automatically." I have spent a several hours researching this but have not been able to fix. So I wanted to make sure it wasn't something in the way I have things set up in cPanel. Thanks in advance for any assistance. Russ Quote Link to comment Share on other sites More sharing options...
TCH-Bruce Posted November 18, 2009 Share Posted November 18, 2009 A hash bang is the first line in a script file that lists the path to the interpreter to be used. For Perl you would use: >#!/usr/bin/perl For PHP you would use: >#!/usr/bin/php Quote Link to comment Share on other sites More sharing options...
Russ Posted November 18, 2009 Author Share Posted November 18, 2009 A hash bang is the first line in a script file that lists the path to the interpreter to be used. For Perl you would use: >#!/usr/bin/perl For PHP you would use: >#!/usr/bin/php Thanks, Bruce. I am curious, is the "the path to the interpreter" the directory on the server where php is installed? Also, does it matter where the script is saved? Does it need to be on cgi or scgi, or can I put it into another directory? Some information I have been looking at on the internet suggests that for security purposes, I should not put it in my /public_html directory. Thanks, again. Russ Quote Link to comment Share on other sites More sharing options...
TCH-Bruce Posted November 18, 2009 Share Posted November 18, 2009 "the path to the interpreter" is /usr/bin and the interpreter in this case is php. The hash bang tells the operating system what program the script requires to run. See the Wiki page. The script can be in any folder but usually placed in the cgi-bin folder. It doesn't matter if it's a perl or php script. Quote Link to comment Share on other sites More sharing options...
Russ Posted November 18, 2009 Author Share Posted November 18, 2009 (edited) Thanks,again, Bruce. For my edification, would running phpinfo() have told me the "the path to the interpreter"? I am trying to learn my way around things better, and to peice together how php is installed and configured on TCH. Also, I have been searching for more info on how the piping works and came accross this older thread on TCH forums - Run Php Script To Parse E-mails (from 2004/05). It seems to suggest the use of email filtering rather than email forwarding. I assume that cPanel has changed since the date(s) of that thread and that forwarding is the proper way to accomplish this now on cPanel? BTW - It is interesting...the above thread talks about the exact topic that I have been trying to read up on, but when I searched the TCH forums, it did not come up - or at least it did not come up anywhere near the top of any of the 200+ results yeilded by each of my numerous queries on the forum search field. I discovered the thread on the sixth page of a google seach for "php tutorial stdin pipe" - I was trying to read up on "STDIN" and "php://stdin" and how they related and should be used with piping. I must not be using the seach function correctly, or otherwise it must not be very functional/useful, as it doesn't seem to filter results very well (gives whay to moany and too many that seem off-point) - given my techncal prowess, I'd probably chalk it up to user error! Thanks, again. Edited November 18, 2009 by Russ Quote Link to comment Share on other sites More sharing options...
TCH-Bruce Posted November 18, 2009 Share Posted November 18, 2009 Most of *nix interpreters reside in /usr/bin or /usr/local/bin As for searches, I rarely have success using the search in the forum. But Google can find most anything. Quote Link to comment Share on other sites More sharing options...
Russ Posted November 19, 2009 Author Share Posted November 19, 2009 (edited) I am stumped. I know that this isn't a php site or forum, but I thought that perhaps someone might be able to point out where I am running afoul. I have spent many hours over the last few days trying to figure out how to pipe an email from my TCH domain to a php script so that the script could process it. I have the following code for my script: > #!/usr/bin/php-q <?php // read from stdin $fd = fopen("php://stdin", "r"); $email = ""; while (!feof($fd)) { $email .= fread($fd, 1024); } fclose($fd); // handle email $lines = explode("\n", $email); // empty vars $from = ""; $subject = ""; $headers = ""; $message = ""; $splittingheaders = true; for ($i=0; $i < count($lines); $i++) { if ($splittingheaders) { // this is a header $headers .= $lines[$i]."\n"; // look out for special headers if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) { $subject = $matches[1]; } if (preg_match("/^From: (.*)/", $lines[$i], $matches)) { $from = $matches[1]; } } else { // not a header, but message $message .= $lines[$i]."\n"; } if (trim($lines[$i])=="") { // empty line, header section has ended $splittingheaders = false; } } mail('Me@MyDomain.net', $subject , $message); ?> I have: Configured email forwarding for one of the email addresses for my domain (adc@xyz.net) using cPanel - setting the destination as "Pipe to a Program." Upload and tried the script in cgi-bin and another directory. Set permissions to 755. Tested the script by having it read a from a file (rather than having data piped to it via email) and to works. Tested the script with "#!/usr/bin/php-q", "#!/usr/bin/php -q" and "#!/usr/bin/php" as the Hashbang, all with no luck. But, alas the script does not work. Each time I have forward an email to the email address (adc@xyz.net) that is configured to pipe to the script, I get the following bounce back email: This message was created automatically by mail delivery software. A message that you sent could not be delivered to one or more of its recipients. This is a permanent error. The following address(es) failed: pipe to |/home/mycpanelusername/cgi-bin/pipemail.php generated by adc@xyz.net local delivery failed Moreover, no email is sent to Me@MyDomain.net ( the final action of the script). Does anyone have any suggestions as to what I am missing? Any help would be appreciated. Thanks. Russ Edited November 19, 2009 by TCH-Dick changed [code] to [php] for easier reading Quote Link to comment Share on other sites More sharing options...
TCH-Dick Posted November 19, 2009 Share Posted November 19, 2009 Try setting your hashbang to #!/usr/local/bin/php -q If that doesnt work, please drop in a ticket and we will gladly take a look. Quote Link to comment Share on other sites More sharing options...
Russ Posted November 19, 2009 Author Share Posted November 19, 2009 Try setting your hashbang to #!/usr/local/bin/php -q If that doesnt work, please drop in a ticket and we will gladly take a look. Thanks, Dick. I tried #!/usr/local/bin/php -q as the hashbang, but it did not work. So, as you've suggested, I have submitted a ticket. Quote Link to comment Share on other sites More sharing options...
Russ Posted November 19, 2009 Author Share Posted November 19, 2009 Okay. I have heard back from the help desk. But they were not able to get this working. They said that my email pipe in cPanel was not set correctly. Instead of: /home/*******/cgi-bin/pipemail.php They said it should be: /home/*******/home/*******/public_html/cgi-bin/pipemail.php They re-set my email pipe in cPanel accordingly. However, the script/piping still does not work, and I get a similar baunce back email as described above. The help desk says that the problem is with the script and stated: I have checked the error logs for email send, it is given below. This is due to script errors, mainly due to improper hashbang in the beginning of script. I have tried to solve it, but was not able to. Please note that we have only very limited support for scripting issues. So, kindly verify your script with any web design expert. |/home/******/home/******/public_html/cgi-bin/pipemail.php |(testphp@mydomain.net) <testphp@mydomain.net> R=virtual_aliases_nostar |T=virtual_address_pipe: Child process of virtual_address_pipe transport |returned 127 (could mean unable to exec or command does not exist) from |command: /home/******/home/******/public_html/cgi-bin/pipemail.php I tested the script by modifying it to read from a txt file instead of "php://stdin", and load modified script and a test text file onto my site and ran it in the browser. That worked fine and the email at the end of the script was sent and received. So I imagine that the problem is either in the pipe set up or the hashbang. But I am at a loss to understand. Any "web design experts" out there that can help out this lost noobie? Russ Quote Link to comment Share on other sites More sharing options...
TCH-Dick Posted November 19, 2009 Share Posted November 19, 2009 I have your script working on my server using the code your posted before my last reply. The only thing I changed was the hashbang and of course the email address. I have now located your ticket and I am going to review your setup. Quote Link to comment Share on other sites More sharing options...
TCH-Dick Posted November 19, 2009 Share Posted November 19, 2009 The pipe to /home/*******/home/*******/public_html/cgi-bin/pipemail.php was an error on our part and you had it setup properly. The extra /home/*******/ is because cPanel now adds that part of the path for you. As for the script itself, the only issue I found was the hashbang and I made the following changes: >//changed #!/usr/local/bin/php-q <?php //to #!/usr/local/bin/php -q <?php The working script is at /home/*******/public_html/pipemail.php with the appropriate forwarder setup. Please give it a spin and let me know if you have any problems. Quote Link to comment Share on other sites More sharing options...
Russ Posted November 20, 2009 Author Share Posted November 20, 2009 The pipe to /home/*******/home/*******/public_html/cgi-bin/pipemail.php was an error on our part and you had it setup properly. The extra /home/*******/ is because cPanel now adds that part of the path for you. As for the script itself, the only issue I found was the hashbang and I made the following changes: >//changed #!/usr/local/bin/php-q <?php //to #!/usr/local/bin/php -q <?php The working script is at /home/*******/public_html/pipemail.php with the appropriate forwarder setup. Please give it a spin and let me know if you have any problems. Okay, I can confirm that I did in fact receive the emails. But, alas, I cannot get this to work. Here is what I did, maybe I messed something up... Since you got it to work with a script saved in public_html, I wanted to see if it worked when the script was saved in a folder under public_html, so I changed the pipe that you set up to be as follows - /home/*******/public_html/php_test/pipemail.php, with the same script. And, I got the same bounced back email I had been getting. So, I changed the pipe back to what you had - /home/*******/public_html/pipemail.php - and I didn't get the bounce back email, but unlike in your case, I did not get any emails! What am I doing wrong? Thank you for your patience. Russ Quote Link to comment Share on other sites More sharing options...
TCH-Dick Posted November 20, 2009 Share Posted November 20, 2009 I copied /home/*******/public_html/pipemail.php to /home/*******/public_html/php_test/pipemail.php and then changed the pipe to match and received no bounce. You should have received the same email from this test. The only other issue that could exist is what and how you created the file. When I first tested this on my server, I had some unneeded spaces in the script at the beginning and end from using wordpad instead of notepad. Once I edited the script on the server and removed the spaces, all worked as intended. Quote Link to comment Share on other sites More sharing options...
Russ Posted November 20, 2009 Author Share Posted November 20, 2009 (edited) I copied /home/*******/public_html/pipemail.php to /home/*******/public_html/php_test/pipemail.php and then changed the pipe to match and received no bounce. You should have received the same email from this test. The only other issue that could exist is what and how you created the file. When I first tested this on my server, I had some unneeded spaces in the script at the beginning and end from using wordpad instead of notepad. Once I edited the script on the server and removed the spaces, all worked as intended. Thanks, Dick. The problem that I am having now is this...I get your emails from the script, but not my own. If I send an email to the piped email, I don't get a bounce back error, but I don't get an email from the script either? Strange. Edited November 20, 2009 by Russ Quote Link to comment Share on other sites More sharing options...
Russ Posted November 20, 2009 Author Share Posted November 20, 2009 Thanks, Dick. The problem that I am having now is this...I get your emails from the script, but not my own. If I send an email to the piped email, I don't get a bounce back error, but I don't get an email from the script either? Strange. GOT IT! Lost the forest thru the trees...I had the email for the pipe set up as phptest@mydomain.net, but was sending email to testphp@mydomain.net. Thank you so much, Dick. I appreciate you putting up with my ignorance! Russ Quote Link to comment Share on other sites More sharing options...
TCH-Dick Posted November 20, 2009 Share Posted November 20, 2009 You're welcome! I know what it's like getting stuck on the small things, especially after you have been staring at the same code for too long. Quote Link to comment Share on other sites More sharing options...
Russ Posted November 20, 2009 Author Share Posted November 20, 2009 (edited) You're welcome! I know what it's like getting stuck on the small things, especially after you have been staring at the same code for too long. I am curious, in reconstructing what my issues were with the script, it seems that I had problems setting up the hash bang and the email pipe properly and with white spaces in the script. In the latter instance, would errors have been logged somewhere? I am trying to figure out what was wrong with the white space. In particular, it was my understanding, based on the research I have been doing, that white space before the opening php tag (<?php) or after the closing php tag (?>) would cause errors that would prevent the script from working, but that white space between the tags was fine. After I had gotten my hash bang correct and the email pipe set up properly, I still had a problem with the script - I was getting bounce back emails telling me it did not work. That problem was "fixed" when I removed the blank line in line 3 of the above quoted script (see post #7, above). An except of the script follows: > #!/usr/bin/php-q <?php // read from stdin $fd = fopen("php://stdin", "r"); $email = ""; while (!feof($fd)) { $email .= fread($fd, 1024); } //BALANCE OF THE SCRIPT OMITTED - SEE POST #7 FOR ENTIRE SCRIPT ?> Once I removed line #3, as you suggested, the script worked as I intended. I am trying to determine why that blank space was was causing a problem. I had originally cut and pasted that part of the script from this tutorial. I cannot remember if I pasted it into notebook or wordpad. Could that have caused this issue? Could one of those apps have inserted some sort of formatting that was interpreted as improper code when the script ran? I thought that if there was an error log and I checked it, it might give a clue. Any ideas? Thanks, again. Edited November 20, 2009 by Russ Quote Link to comment Share on other sites More sharing options...
TCH-Dick Posted November 20, 2009 Share Posted November 20, 2009 I checked the server logs and there were two errors that occured, "malformed header from script" and "Exec format error". Both of which only appeared when you ran the php script via cgi-gin. I was unable to locate any other errors, as the white space and hasbang errors didnt prevent the script from running, it just didn't run everything. As for the white space, I had the same problem at the hasbang and opening tag, I also had to remove the windows style carriage return added by wordpad. Quote Link to comment Share on other sites More sharing options...
Russ Posted November 21, 2009 Author Share Posted November 21, 2009 I checked the server logs and there were two errors that occured, "malformed header from script" and "Exec format error". Both of which only appeared when you ran the php script via cgi-gin. I was unable to locate any other errors, as the white space and hasbang errors didnt prevent the script from running, it just didn't run everything. As for the white space, I had the same problem at the hasbang and opening tag, I also had to remove the windows style carriage return added by wordpad. Thanks, Dick. I went back and looked over the bounce back emails - and it seems that some of them had error messages buried in the toward the bottom. Well, thanks again. I really do appreciate your help. Your customer service is excellent! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.