Rowno Posted April 29, 2008 Share Posted April 29, 2008 I'm trying to send a copy of all emails received at one address to 2 other addresses by piping the emails to the following code but it doesn't work, can anyone help? #!/usr/bin/php <?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 $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]; } } else { // not a header, but message $message .= $lines[$i]."\n"; } if (trim($lines[$i])=="") { // empty line, header section has ended $splittingheaders = false; } } mail("email1", $subject, $message, $headers); mail("email2", $subject, $message, $headers); ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 29, 2008 Share Posted April 29, 2008 "It doesn't work" covers a lot of ground. Please explain how you're doing invoking the script, any errors you're getting, etc ... Ken Quote Link to comment Share on other sites More sharing options...
Rowno Posted April 29, 2008 Author Share Posted April 29, 2008 I'm using my host's cpanel to pipe all emails received at one address to the script above. If a piping script receives an error, does it create a normal php error? or something else? I have received no php errors from the script that I know of, so I guess the headers aren't being stripped properly which is leading to the new emails not being sent properly. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 29, 2008 Share Posted April 29, 2008 When debugging something like this, I usually write timestamped debug lines to a file at critical places in the script that way you can tell what's going on. Since I run cPanel also, I can take your code and see what it does on my host. Ken Quote Link to comment Share on other sites More sharing options...
Rowno Posted April 29, 2008 Author Share Posted April 29, 2008 Thanks for the help, I was trying to think of a way to see what was happening in the script since I couldn't just echo out the variables in the script. How would I write these debug lines, since it might be handy to know how to do that? Also the address that is being piped is still receiving emails sent to it if that is any help. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 30, 2008 Share Posted April 30, 2008 I see my last post got lost... Did you make your script executable? Also, I changed the first line in the script to #!/usr/bin/php -f Which will suppress any messages from PHP. If you don't do this, you will get a message indicating that the email didn't work, even if it did. Ken Quote Link to comment Share on other sites More sharing options...
Rowno Posted April 30, 2008 Author Share Posted April 30, 2008 When I added the pipe to the cpanel, the cpanel said the Hashbang was missing or incorrect and gave me the option to automatically fix it so I clicked that, then the cpanel said the Hashbang was fixed/added and the script was made executable. This is what the cpanel added: #!/usr/local/bin/php -q Also, I have checked my domain's default address and I've found no error messages. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 30, 2008 Share Posted April 30, 2008 Ok, did that help? I've discovered that if you write anything out using the echo statement, you will get a "bounced" message back with the strings written in the message. If you don't, then the script isn't being invoked at all. Ken Quote Link to comment Share on other sites More sharing options...
Rowno Posted April 30, 2008 Author Share Posted April 30, 2008 Ok, I'll try that. Quote Link to comment Share on other sites More sharing options...
blueman378 Posted April 30, 2008 Share Posted April 30, 2008 Just a question, your using this for the contact us form right? well why dont you add a bcc header? Quote Link to comment Share on other sites More sharing options...
Rowno Posted April 30, 2008 Author Share Posted April 30, 2008 I don't think the emails are reaching the script, how do you make it executable? And for some when I send an email using my default email account in outlook to the piped address, the email gets returned to me with no changes to it. Quote Link to comment Share on other sites More sharing options...
Rowno Posted April 30, 2008 Author Share Posted April 30, 2008 Ok, I managed to get this to appear in the cpanel: All email sent to admin@webspirited.com will now be copied to |/home/webspiri/pipes/forwardpipe.php The program you have chosen to pipe to "home//home/webspiri/pipes/forwardpipe.php" has the following problems: needs to be executable. To correct this, please click Fixup Piped Program. Is this a good sign? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 30, 2008 Share Posted April 30, 2008 If you have shell access to your account, you can do chmod u+x yourscriptname.php If you don't have shell access, you will have to use either an FTP program or the File Manager in the cPanel. If you don't know how to use the File Manager, check with your host's support. Ken Quote Link to comment Share on other sites More sharing options...
blueman378 Posted April 30, 2008 Share Posted April 30, 2008 would you liek me to chmod it rowno? Quote Link to comment Share on other sites More sharing options...
Rowno Posted April 30, 2008 Author Share Posted April 30, 2008 The script has now been made fully executable and I added in an echo along with your thing at the top and i'm still not getting any emails back from it. Quote Link to comment Share on other sites More sharing options...
Rowno Posted April 30, 2008 Author Share Posted April 30, 2008 I think the pipe is working now but the script isn't doing what it's supposed to be doing. Instead, any emails sent from the 2 addresses mentioned in the mail functions at the bottom of the script are being sent back to the address they were sent from. Any emails sent from any other address are not sent back. Here's the script again: #!/usr/local/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 $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]; } } else { // not a header, but message $message .= $lines[$i]."\n"; } if (trim($lines[$i])=="") { // empty line, header section has ended $splittingheaders = false; } } mail("email1", $subject, $message, $headers); mail("email2", $subject, $message, $headers); ?> 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.