Jump to content

Question Regarding Whitespace Problem in PHP Script


gatoruss

Recommended Posts

I am writing a script to which I am piping to an email.  The script reads the email as STDIN and parses the email - saveing headers and body to different variables.  Then to show it worked, the script sends an email back to me with info from the piped email.  I am doing this as a learning exercise.  I have run into the following issue (that I have solved), but do not understand why it was a problem...tried researching on net and haven't found something on point that explains issue.  With that back ground, here goes:

 

My code is as follows:

 

#!/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
$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('ABCABC@XYZXYZ.net', $subject , $message);
?>

 

When I sent an email to the piped email address, the script did not run and I received an error/bounce back email stating:

 

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/**********/public_html/test/emailscr.php

    generated by PipedEmail@MyDomain.net

    local delivery failed

 

However, if I remove the white space immediately after the opening php tag (<?php), the script ran as designed.

 

I am confused, because I thought that white spaces before or after the tags (i.e., outside of tags) was the issue, and that white spaces inside the tags was fine?

 

Can someone help out this Noobie and steer me toward an explanation?

 

Thanks!

Link to comment
Share on other sites

I am not sure if this is the case, but it could have something to do with headers etc. if you read up on headers you cannot have output of any kind (whitespaces etc) before header calls, if you do it ends the header right there and an error.

 

Like I said, I am not sure if that is what is happening, but it sure sound like it and somewhere along the way there are headers trying to be sent or needing to be sent and that is preventing it thus causing the error...

Link to comment
Share on other sites

I am not sure if this is the case, but it could have something to do with headers etc. if you read up on headers you cannot have output of any kind (whitespaces etc) before header calls, if you do it ends the header right there and an error.

 

Like I said, I am not sure if that is what is happening, but it sure sound like it and somewhere along the way there are headers trying to be sent or needing to be sent and that is preventing it thus causing the error...

 

Thank you.

 

I had considered that, but I thought that the "-q" argument in the hashbang turned of the header output of the php script?

 

I will read further on "Headers" starting with the link you suggested.

 

Thanks again.

Link to comment
Share on other sites

It was my understanding that my script was running as from the command line.  So I was looking at information on http://us.php.net/manual/en/features.commandline.php.  Interestingly, in an example for a script that runs from the command line, the following was given:

 

Example #8 Script intended to be run from command line (script.php)

 

#!/usr/bin/php

<?php

 

if ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {

?>

 

This is a command line PHP script with one option.

 

  Usage:

  <?php echo $argv[0]; ?> <option>

 

  <option> can be some word you would like

  to print out. With the --help, -help, -h,

  or -? options, you can get this help.

 

<?php

} else {

    echo $argv[1];

}

?>

 

That example has the same blank line after the opening tags.  I reviewed the various user comments and sample code on th epage, and some had the white space and others did not.  But I could find no mention of the issue, or whether the white space would or could be an issue. :confused:

Link to comment
Share on other sites

I have been digging around some more, I think that I figured out what my issue had been.  Thought I should post a description in case some noobie like me comes across a similar issue.

 

I had been following a tutorial at the following site http://evolt.org/incoming_mail_and_php?from=0&comments_per_page=50, and I had copied & pasted some code from that webpage into notepad.  I subsequently edited the file in notepad++.

 

Since the file I created  (when I saved the script) was done in windows, newlines (and blank lines) were indicated in Notepad and Notepad++ by a CRLF.  So when I upload to my server, which was Linux, there was a CR character at the end of each line - apparently, Linux indicates a newline (or a blank line) with LF.

 

So the problem was that every line had an extra character (CR), which was causing errors with the script.  I had been editing the script file online thru cPanel, to remove the blank line referenced above and when I saved the file via the cPanel editor, the file was converted to Linux format - removing all of the CR characters.

 

Therefore, while it seemed like removing the blank line was fixing the issue, what really fixed the issue was saving the file in Linus format and thereby removing the CR characters!

 

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.