phpjeff Posted October 29, 2010 Share Posted October 29, 2010 Hi. I am brand new here... I have a script where I'm trying to parse an email and everything is working fine but it looks like the percent sign (%) is getting stripped out of my message. The percent sign is in the subject message and it gets removed and I also can't use it in any other strings in the script. I've tried just adding a slash in front like \% but that didn't fix it. Is there another way to escape a percent sign when using it in a script? Any help is appreciated. Thanks. -Jeff Quote Link to comment https://forums.phpfreaks.com/topic/217241-problem-escaping-a-percent-sign/ Share on other sites More sharing options...
Ninjakreborn Posted October 29, 2010 Share Posted October 29, 2010 Post your full code here, please. Quote Link to comment https://forums.phpfreaks.com/topic/217241-problem-escaping-a-percent-sign/#findComment-1128155 Share on other sites More sharing options...
phpjeff Posted October 29, 2010 Author Share Posted October 29, 2010 this script reads the stdin from an email address piped to this script. all that is working fine. i can get the output and send on to another email address. however the subject that contains a % sign gets everything removed after it encounters the % sign. Any help is appreciated. #!/usr/local/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 $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]; // NEED TRIM? } 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; } } // FIX THE % SIGN #$subject = str_replace("TAG=",addslashes("%"),$subject); // DOESN'T WORK #$subject = str_replace("TAG=","\%",$subject); // DOESN'T WORK // WRITE TEST OUTPUT $outFile = fopen("output.txt","w"); fwrite($outFile,$subject."\n\n".$message); fclose($outFile); ?> Quote Link to comment https://forums.phpfreaks.com/topic/217241-problem-escaping-a-percent-sign/#findComment-1128159 Share on other sites More sharing options...
Ninjakreborn Posted October 29, 2010 Share Posted October 29, 2010 Your going to need to throw in some debugging text. After each main action, echo out the subject. Echo it right after the Subject is received, again after the regular expression work, and again right after the mail sends. Try a combination of echo's. The first step is to identify, where in your script the subject is being messed up. First verify you are receiving the subject correctly to begin with. Post your results here. Quote Link to comment https://forums.phpfreaks.com/topic/217241-problem-escaping-a-percent-sign/#findComment-1128162 Share on other sites More sharing options...
phpjeff Posted October 29, 2010 Author Share Posted October 29, 2010 Hi. Thanks. I added a line to store the output from the sent in email into a txt file... And it looks like it is throwing in a return right before the % sign for some reason. And then... the regex code is looking for the text "Subject: " and then stopping at the next return. So it's just missing it. But it's there. So now I've got to rewrite the regex that is parsing the 2 lines where the subject is going. I don't know why the percent sign is making it go onto a new line but that appears to be the issue. here's part of what that looks like: X-Google-Sender-Auth: JP_8kO9s1KH-KnYGz32kaEk Message-ID: <[email protected]> Subject: A BUNCH OF SAMPLE TEXT * more stuff here %tag1 tag2 To: sample person <email> Content-Type: text/plain; charset=ISO-8859-1 [/php the lines parsing the Subject out from the big mess of email text looks like this: [code=php:0] if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) { #print_r($matches); $subject = $matches[1]; // } So that is what I'll try changing to also include the next return. I'll post back here. Quote Link to comment https://forums.phpfreaks.com/topic/217241-problem-escaping-a-percent-sign/#findComment-1128164 Share on other sites More sharing options...
Ninjakreborn Posted October 29, 2010 Share Posted October 29, 2010 Try what you mentioned. If it does not work, post back here. If you can't get the issue resolved, or still need help, then send me a personal message and I will take a look at the script for you myself. Quote Link to comment https://forums.phpfreaks.com/topic/217241-problem-escaping-a-percent-sign/#findComment-1128167 Share on other sites More sharing options...
phpjeff Posted October 30, 2010 Author Share Posted October 30, 2010 In case this helps anyone else... Turns out the issue was not anything with the code escaping percent signs etc... The issue is that reading stdin from an email... the subject line gets shoved to 2 lines if it exceeds the limit on however many characters per line etc. So... I started noticing that other things were getting moved down to line 2 and that clued me in. So... I am not a regex expert so I just whipped up the below code to parse out the Subject and then put it all back onto one line. It's working fine now. Thanks for all your help! $split1 = explode("To: ",$email); // SPLIT AT FIRST To: #echo'<pre>';print_r($split1);echo'</pre>'; $split2 = explode("Subject: ",$split1[0]); // SPLIT AT FIRST Subject: #echo'<pre>';print_r($split2);echo'</pre>'; $subject = $split2[1]; $subject = str_replace("\n","",$subject); // FIX RETURN PROBLEM #echo'<pre>';print_r($subject);echo'</pre>'; // THIS WORKS EVEN IF THERE ARE ADDITIONAL TEXTS IN THE MESSAGE To: OR Subject: Quote Link to comment https://forums.phpfreaks.com/topic/217241-problem-escaping-a-percent-sign/#findComment-1128350 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.