Jump to content

problem escaping a percent sign


phpjeff

Recommended Posts

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

Link to comment
Share on other sites

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



?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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: <AANLikoon9pzxSrr4R2HXQ7SQRD_YoAJF=esb4qW-@mail.gmail.com>
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.

Link to comment
Share on other sites

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: 


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.