mattisl Posted November 16, 2004 Posted November 16, 2004 Is it possible to trigger a PHP-script when an e-mail has been received on a specific address? E.g when a mail has been sent to automatic@**** I want the content to be parsed by one of my php script automatically. Can this be done? / Mattis Quote
borfast Posted November 16, 2004 Posted November 16, 2004 Mattis, check the "E-Mail Filtering" link under the "E-Mail" section on your cPanel. You can use something as "|/home/user/email.php" as the destination. Hope this helps Quote
mattisl Posted November 16, 2004 Author Posted November 16, 2004 Looks exactly like the thing I need However I couldn't quite figure out how the destination works. Lets say I have my php-parser called "parser.php" in a subfolder called "myfolder" (the full url would be http://www.example.com/myfolder/parser.php). What should the destination text look like? I've tried: "|/home/myusername/myfolder/parser.php" "|/home/myusername/public_html/myfolder/parser.php" "|/myfolder/parser.php" "|/public_html/myfolder/parser.php" But without success. Quote
MikeJ Posted November 16, 2004 Posted November 16, 2004 |/home/myusername/public_html/myfolder/parser.php Make sure not to put quotes in there. Also, this will run as a shell script, not a web page, so the script has to know how to execute, which means the script has to be set to executable (go into file manager and make sure the execution bits are set) and the first line of the script needs to be #!/usr/bin/php so the script knows what to run to parse it. The email will be passed to the script via STDIN (standard input). Quote
mattisl Posted November 16, 2004 Author Posted November 16, 2004 Still can't get it working This is how I set it up: Destination: |/home/myusername/public_html/myfolder/parser.php Then I set the parser.php permissions to 744. My parser.php file looks exactly like this:; >#!/usr/bin/php <? mail("me@****","E-mail parsing test!","Seems to work...",""); ?> When I run the filter test in Cpanel it shows this: Filter Trace Results: Condition is true: $header_to: contains me@**** Return-path copied from sender Sender = myusername@server64.totalchoicehosting.com Recipient = myusername@server64.totalchoicehosting.com Testing Exim filter file "/etc/vfilters/****" Pipe message to: /home/myusername/public_html/myfolder/parser.php Filtering set up at least one significant delivery or other action. No other deliveries will occur. ------------ If all works right I should receive an e-mail at me@**** sent from the php-script, this mail however doesn't get sent =/. Going directly to http://www.example.com/myfolder/parser.php works. Thanks for your help so far, the support is great as always Rock Sign Quote
MikeJ Posted November 16, 2004 Posted November 16, 2004 (edited) Then I set the parser.php permissions to 744. Try with permissions 755 (so that all can execute). And something unrelated to it working, but you can change that first line to !#/usr/bin/php -q to suppress the header output since this will only be used in your filters (assuming that's the case). Edited November 16, 2004 by TCH-MikeJ Quote
mattisl Posted November 17, 2004 Author Posted November 17, 2004 The script runs now when I send an e-mail to me@****. However I get a "Mail delivery failure" e-mail back to the address I sent the first e-mail from. I understand that I need to return some kind of value that indicates that the script was successful, which value should be returned? Also I wasn't able to read the e-mail from STDIN. This is my code so far: >#!/usr/bin/php <? if(!defined("STDIN")) define("STDIN", "fopen('php://stdin','r')"); $message = fread(STDIN, 1024); mail("mattis@gamalog-media.com","E-mail parsing test!","The message:\n".$message,""); return true; ?> Quote
borfast Posted November 17, 2004 Posted November 17, 2004 This is just a wild guess, since I never tried doing what you are doing but have you tried returning 0, just like in C, C++, etc? Quote
mattisl Posted November 17, 2004 Author Posted November 17, 2004 Changing the return value to 0 didn't change anything I checked the delivery failure e-mail again and found this error: Warning: fread(): supplied argument is not a valid stream resource in /home/myusername/public_html/myfolder/parser.php on line 5. Line 5 is: $message = fread(STDIN, 1024); Quote
Flashl Posted November 24, 2004 Posted November 24, 2004 I had an open ticket with the help desk about achieving the same type of email aliasing/forwarding via a php script. Initially, I used cpanel’s email filtering but was later advised by the help desk to use cpanel’s forwarding instead of filtering. I have tried various strings in cpanel’s mail->mail manager->Forwarders section. The last session went something like this: _______________________________________________________________ Add a new Forwarder All email sent to mailpublisher@**** will now be redirected to 'pipe "/home/mydomain/www/mail.php"' Forwarding Maintenance Current Forwarders mailpublisher@**** 'pipe"/home/mydomain/www/mail.php"'@**** ________________________________________________________________ Needless to say, the forwarder as it is stored by cpanel with its automated appendage of @**** at the end of the pipe command does not work. If filtering is not the answer and forwarding is not the answer, what is the answer to this problem? Quote
TCH-Bruce Posted November 24, 2004 Posted November 24, 2004 Forwarding is not going to do it. You need to add a filter. Then in the where to filter box enter something like this. |/home/cpanelname/path to script Note that the "|" is the pipe symbol. Quote
borfast Posted November 24, 2004 Posted November 24, 2004 I'd say you should open a Help Desk ticket and see if the techs can figure out what's the answer. Quote
Flashl Posted November 24, 2004 Posted November 24, 2004 Thanks for the help. Now something new has occurred. I created the alias as follows: Add Filter A filter has been added that sends all mail header_to: that contains mailpublisher@**** |/home/myacct/www/mail.php Filter Trace Filter Trace Results: Condition is true: $header_to: contains mailpublisher@**** Return-path copied from sender Sender = myacct@server86.totalchoicehosting.com Recipient = myacct@server86.totalchoicehosting.com Testing Exim filter file "/etc/vfilters/****" Pipe message to: /home/myacct/www/mail.php Filtering set up at least one significant delivery or other action. No other deliveries will occur. When I send email from a local account on my PC, I received the following from mydomain: To: Pcuser@cox.netSubject: Mail rejected From: MyDomainName <admin@****> Date: Wed, 24 Nov 2004 14:41:22 -0500 Content-Type: text/plain; charset=iso-8859-1 X-Mailer: GeekLog 1.3.10rc2 Message-Id: <E1CX310-0006a8-N3@server86.totalchoicehosting.com> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server86.totalchoicehosting.com X-AntiAbuse: Original Domain - cox.net X-AntiAbuse: Originator/Caller UID/GID - [32310 32311] / [47 12] X-AntiAbuse: Sender Address Domain - server86.totalchoicehosting.com X-Source: X-Source-Args: X-Source-Dir: And the following: 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/myacct/www/mail.php generated by mailpublisher@**** pipe to |/home/myacct/www/mail.php generated by admin@**** The following text was generated during the delivery attempts: ------ pipe to |/home/myacct/www/mail.php generated by mailpublisher@**** ------ Content-type: text/html X-Powered-By: PHP/4.3.9 Set-Cookie: LastVisit=1101325282; expires=Thu, 24-Nov-05 19:41:22 GMT; path=/; domain=.**** Set-Cookie: LastVisitTemp=deleted; expires=Tue, 25-Nov-03 19:41:21 GMT; path=/; domain=.**** ------ pipe to |/home/myacct/www/mail.php generated by admin@**** ------ Content-type: text/html X-Powered-By: PHP/4.3.9 Set-Cookie: LastVisit=1101325282; expires=Thu, 24-Nov-05 19:41:22 GMT; path=/; domain=.**** Set-Cookie: LastVisitTemp=deleted; expires=Tue, 25-Nov-03 19:41:21 GMT; path=/; domain=.**** ------ This is a copy of the message, including all the headers. ------ Return-path: <PCUser@cox.net> Received: from [68.230.240.27](helo=lakermmtao12.cox.net) by server86.totalchoicehosting.com with esmtp (Exim 4.43) id 1CX30z-0001qE-HY; Wed, 24 Nov 2004 14:41:21 -0500 Received: from i9ez64k6sl0 ([24.254.123.135]) by lakermmtao12.cox.net (InterMail vM.6.01.04.00 201-2131-117-20041022) with SMTP id <20041124194128.TTAT8344.lakermmtao12.cox.net@i9ez64k6sl0>; Wed, 24 Nov 2004 14:41:28 -0500 Message-ID: <003a01c4d25d$99eaa620$7102a8c0@i9ez64k6sl0> Reply-To: "PCUser" <PCUser@cox.net> From: "PCUser" <PCUser@cox.net> To: <mailpublisher@****> Cc: <admin@****> Subject: a good idea. Date: Wed, 24 Nov 2004 13:41:32 -0600 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 Publishing by email is a good idea. Quote
MikeJ Posted November 24, 2004 Posted November 24, 2004 Any output from the script will be interepreted as an error. To have the script process without getting a failure email bounced back, use #!/usr/bin/php -q as the first line of the script (defines the processor, and -q supresses header output) and make sure there are no blank lines outside of your PHP block (because they will output as blank lines if you do). For example: >#!/usr/bin/php -q <?php ... your php code here ... ... blank lines in this block are ok. blank lines outside of it are not. ... ?> Make sure outside of the <?php and ?> there are no blank lines. Quote
MikeJ Posted November 24, 2004 Posted November 24, 2004 (edited) Also I wasn't able to read the e-mail from STDIN. This is my code so far: >#!/usr/bin/php <? if(!defined("STDIN")) define("STDIN", "fopen('php://stdin','r')"); $message = fread(STDIN, 1024); mail("mattis@gamalog-media.com","E-mail parsing test!","The message:\n".$message,""); return true; ?> Your syntax is a little off. If you want to read in an email (following the direction of your use of define()), you would want something more like this: >// read from stdin define("STDIN", fopen("php://stdin", "r")); $message = ""; while (!feof(STDIN)) { $message .= fread(STDIN, 1024); } fclose(STDIN); Most importantly, your main problem is the quotes around the fopen() function. In your code, you are defining STDIN as the string "fopen('php://stdin','r')" instead of the result of the fopen() function. Also, without the while loop, you will only get the first 1,024 characters of your message. Edited November 24, 2004 by TCH-MikeJ Quote
Flashl Posted November 24, 2004 Posted November 24, 2004 Head of mail.php #!/usr/bin/php -q<?php .... Tail of mail.php?> Reply to PcUser acct X-AOL-UID: 441.123479459X-AOL-DATE: Wed, 24 Nov 2004 3:58:59 PM Eastern Standard Time Return-Path: <myacct@server86.totalchoicehosting.com> Received: from rly-xb04.mx.aol.com (rly-xb04.mail.aol.com [172.20.64.136]) by air-xb01.mail.aol.com (v103.7) with ESMTP id MAILINXB11-a941a4f60736c; Wed, 24 Nov 2004 15:58:59 -0500 Received: from server86.totalchoicehosting.com (server86.totalchoicehosting.com [66.235.180.57]) by rly-xb04.mx.aol.com (v103.7) with ESMTP id MAILRELAYINXB49-a941a4f60736c; Wed, 24 Nov 2004 15:58:47 -0500 Received: from myacct by server86.totalchoicehosting.com with local (Exim 4.43) id 1CX4Dk-0007Q1-EJ for PCUser@aol.com; Wed, 24 Nov 2004 15:58:37 -0500 To: PCUser@aol.com Subject: Mail rejected From: Mydomain <admin@****> Date: Wed, 24 Nov 2004 15:58:35 -0500 Content-Type: text/plain; charset=iso-8859-1 X-Mailer: GeekLog 1.3.10rc2 Message-Id: <E1CX4Dk-0007Q1-EJ@server86.totalchoicehosting.com> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server86.totalchoicehosting.com X-AntiAbuse: Original Domain - aol.com X-AntiAbuse: Originator/Caller UID/GID - [32310 32311] / [47 12] X-AntiAbuse: Sender Address Domain - server86.totalchoicehosting.com X-Source: X-Source-Args: X-Source-Dir: X-AOL-IP: 66.235.180.57 Forbidden to post ! Quote
MikeJ Posted November 24, 2004 Posted November 24, 2004 (edited) X-Mailer: GeekLog 1.3.10rc2 Forbidden to post ! That looks like a configuration issue with your Geeklog. The Geeklog is bouncing it, not the system at that point. I haven't used Geeklog myself, so I'm not sure what you need to change to enable the email postings. Edited November 24, 2004 by TCH-MikeJ Quote
Flashl Posted November 24, 2004 Posted November 24, 2004 You were right. PHP mail is the default for geeklog, for whatever reason, when I changed the configuration file to use SMTP mail everything worked great. Thanks for the help Quote
section31 Posted December 6, 2004 Posted December 6, 2004 this was all a very good read. Whats the best way to limit the file size it parses? I wish cpanel would offer that option as a filter, "Size of email" so I could block anything over lets say 512 K. PS: I wrote a little moblog with the help from this thread. Quote
Ismael B. Posted January 24, 2005 Posted January 24, 2005 Excuse me. I'm trying so hard to process my mail in my script. I had followed the entire conversation and unfortunately I can't understand what is Geeklog. Please, please, please, what is Geeklog. Thank you !! Quote
MikeJ Posted January 24, 2005 Posted January 24, 2005 (edited) Geeklog (http://www.geeklog.net/) is weblogging software. It really had nothing to do with processing your mail in a PHP script other than the user in this case was using a PHP written filter to allow posting to his weblog (which was geeklog) via email. Section31, don't know the answer to your question off-hand. I don't write PHP on a regular basis (yet anyway ). Edited January 24, 2005 by TCH-MikeJ Quote
Ismael B. Posted January 26, 2005 Posted January 26, 2005 Greetings to all of you, men !! I have seen that you know something about this. Look, in the DESTINATION field (in CPANEL9 --> E-mail Filtering), I have tried all of this and a lot more: |/home/mbcomp/display.php /home/mbcomp/public_html/cgi-bin/display.php |/home/mbcomp/display/index.php |/home/mbcomp/public_html/cgi-bin/display.php |/mbcomp/display/display.php Where "mbcomp" is my user name. I have one Script in: /public_html/display/index.php and another one (is the same script) in: /public_html/cgi-bin/display.php and /public_html/display.php All of them are the same and just for test they're working. The code in the file index.php and also in display.php is this: >#!/usr/bin/php -q <?php mail("unmillondedolares@gmail.com","YA ESTUVO, WOW!!","SALIÓ LA CHINGADERA"); ?> That, in order to get an email to my personal address just for a TEST. If I go directly to *****/display/index.php it works, cos I receive that email. But I have not received any mail with the subject "YA ESTUVO, WOW!!" when I send an email to ismael@****. I only have received mails that say "MAIL DELIVERY FAIL" in the subject. I wonder if you could mind helping me with any advise you can. Anything will help. It's very important to me. Thank you, man !! Quote
MikeJ Posted February 9, 2005 Posted February 9, 2005 Greetings to all of you, men !! I have seen that you know something about this. Look, in the DESTINATION field (in CPANEL9 --> E-mail Filtering), I have tried all of this and a lot more: |/home/mbcomp/display.php /home/mbcomp/public_html/cgi-bin/display.php |/home/mbcomp/display/index.php |/home/mbcomp/public_html/cgi-bin/display.php |/mbcomp/display/display.php Where "mbcomp" is my user name. I have one Script in: /public_html/display/index.php and another one (is the same script) in: /public_html/cgi-bin/display.php and /public_html/display.php <{POST_SNAPBACK}> Sorry it took me so long to respond to this. I've haven't been around much recently. You have the right concept. |/home/mbcomp/public_html/display/index.php for example is the correct filter for public_html/display/index.php location. I also tried a copy of your test script and had no problem. Try making sure your php script is set to mode 755 (in otherwords, readable and executable by everyone). This can be done in your cPanel file manager by selecting the file, selecting Change Permissions, checking the boxes for read and execute for Group and World / read, write and execute for User, and then hitting save. If that still doesn't work, your hosting provider (you're not hosted at TCH) may have some kind of block on certain types of scripts from sending mail (although probably not likely since it works when you hit the file directly via your browser). You may have to contact your hosts support to see if they'll help you troubleshoot at that point. Quote
Chris Morgan Posted February 17, 2005 Posted February 17, 2005 Make sure the file is in UNIX format. PHP knows how to parse windows format text files, so the script will work when ran through Apache/PHP However when the script is ran via the shell; the shell is trying to parse the firstline and it screws up. I think it will be the case that the script will read the whole file as one long line, as it encounters no UNIX line breaks (\n\r), effectively it will pass >-q\n<?php etc... to PHP which will then ignore all the text after the \n so nothing happens! or (depends on the ' -q' as the space after the /php will cause the above) the shell will try to pipe the email to >#!/usr/local/bin/php\n<?php etc... rather than >#!/usr/local/bin/php and you will get an error. either way it fails. Some FTP clients will do file conversion automatically so some of you will not find this happening Quote
Ismael B. Posted July 3, 2005 Posted July 3, 2005 Hi ! Just to thank you, man. See, I finally find the answer. I put this in the C-panel -> email filtering and it works!! >|/usr/bin/php -q /home/mycpanelsusername/public_html/myfile.php AND THIS IS THE SCRIPT #!/usr/bin/php -q<?php mail("kevin@****","Working...","It worked!"); return 0; ?> I thank you because you tried to help me few months ago. I post this because some people could try to do the same thing than me. Bye, man ! Quote
tima89 Posted October 14, 2005 Posted October 14, 2005 Hello, i have one little question: how do you get rid of the headers, because i can't respond to my clients with all that exra info. (i have put the -q's) Or is the only way to do this is to cut everything except for the message body using php ?? Quote
TweezerMan Posted October 15, 2005 Posted October 15, 2005 Welcome to the forums, tima89! The '-q' in '/usr/bin/php -q' only suppresses output of PHP headers that would normally be sent when outputting to a web browser. If you're wanting to get rid of the e-mail headers, that is something that the script itself would have to do. Quote
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.