Jump to content

Piping emails


Rowno

Recommended Posts

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);

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.