Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 18th, 2008, 10:39 AM
Member
 
Join Date: Sep 2006
Posts: 42
Default Problem with '&' charachter.

Hey.

I have a problem with "&" character. When I try to send a mail from web application and I include '&' to the post the whole message shows empty when send it through. So what can I do to prevent this?

Thanks in advance!
Reply
  #2  
Old August 18th, 2008, 10:54 AM
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 769
Default

Quote:
Originally Posted by zamuel
Hey.

I have a problem with "&" character. When I try to send a mail from web application and I include '&' to the post the whole message shows empty when send it through. So what can I do to prevent this?

Thanks in advance!
Hi,

Can you supply the code that is doing this? If you are using HTML email you could try replacing the & with & which is the code for that character.

If you could post the code it may help people help you more easily.

Cheers
nathj
Reply
  #3  
Old August 18th, 2008, 11:10 AM
Member
 
Join Date: Sep 2006
Posts: 42
Default

Thanks for the quick reply. Yes I will post the code. It basically checks the users registered 'location' and according to that choose e-mail address to be send the post.

Expand|Select|Wrap|Line Numbers
  1.  
  2.         $city = $_GET['city'];
  3.  
  4.     $user = new user();
  5.     $address = $user->SQL->query("select email FROM tblRegios WHERE plaats='".$city."'");
  6.  
  7.     if (sizeof($result = $user->SQL->result())) {
  8.         foreach($result as $key => $val)
  9.         {
  10.             if(isset($val['email']))
  11.                 $email = $val['email'];
  12.         }
  13.     }
  14.  
  15.     $to      = $email;
  16.     $subject = 'Mail';
  17.  
  18.     $headers = 'From';
  19.     $vars = XML_unserialize($_POST['xml']);
  20.  
  21.     mail($to, $subject, $vars['root']['opmerking'], $headers);
  22.  
So, what I want to do is replace '&' with something before sending and then decode it to be '&' again when it is being read.
Reply
  #4  
Old August 18th, 2008, 11:15 AM
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 769
Default

Thanks for posting the code. Where exactly is the & character that's causing the problems?

What purpose is the & serving? Is it simply part of the message? If so could it simply be changed to 'and' using str_replace?

Cheers
nathj
Reply
  #5  
Old August 18th, 2008, 11:40 AM
Member
 
Join Date: Sep 2006
Posts: 42
Default

Yes, simply part of the message. I would like the message keep the same form when received, so & should not be 'and'.
Reply
  #6  
Old August 18th, 2008, 12:00 PM
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 769
Default

Quote:
Originally Posted by zamuel
Yes, simply part of the message. I would like the message keep the same form when received, so & should not be 'and'.
Hi,

In that case try replacing & with & in the message and with HTML email this should then be translated as & when the email client reads it.

I'm not sure why the presence of the & character causes the problem you've got. I would be interested in finding that out.

Cheers
nathj
Reply
  #7  
Old August 18th, 2008, 12:22 PM
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York England :)
Age: 18
Posts: 2,831
Default

Quote:
Originally Posted by nathj
Hi,

In that case try replacing & with & in the message and with HTML email this should then be translated as & when the email client reads it.

I'm not sure why the presence of the & character causes the problem you've got. I would be interested in finding that out.

Cheers
nathj
To do so have a look at str_replace and functions like str_replace.
Reply
  #8  
Old August 18th, 2008, 12:54 PM
Member
 
Join Date: Sep 2006
Posts: 42
Default

Yes, I got the point with that replace function. But for now it seems that it does not work. I does not even work when I try to replace '&' with 'and' as for test. And the fact remains that message text is deleted when '&' mark is posted. This is a very strange problem.
Reply
  #9  
Old August 18th, 2008, 12:56 PM
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 769
Default

Hi,

Could you maybe post the message text? There maybe something in there that is causing the issue.

Cheers
nathj
Reply
  #10  
Old August 18th, 2008, 01:03 PM
Member
 
Join Date: Sep 2006
Posts: 42
Default

Well, the message text can be what ever user chooses to type in, but the text area itself is assigned through line

Expand|Select|Wrap|Line Numbers
  1. $page->assign("textOpmerking",$form->textarea("opmerking","",40,15));
  2.  
and the text are itself is defined in different file like:

Expand|Select|Wrap|Line Numbers
  1.     function textarea($name,$value="",$cols=40,$rows=4,$js = false,$class = false) {
  2.         $id = $name;
  3.         $array = array("name","type","cols","rows","class","id");
  4.         foreach($array as $a) $return .= ($$a!==false) ? " $a=\"".$$a."\"" : "";
  5.         return $this->display($name,"textarea","<textarea ".$return." $js>".((isset($this->values[$name])) ? $this->values[$name] : $value)."</textarea>");
  6.     }
  7.  
Reply
  #11  
Old August 18th, 2008, 02:39 PM
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Age: 24
Posts: 5,306
Default

Heya, Zamuel.

What's an example of a message that is causing a problem? What happens when you try to run a str_replace() on it?
Reply
  #12  
Old August 18th, 2008, 02:54 PM
Member
 
Join Date: Sep 2006
Posts: 42
Default

Well, as an example any of these strings will cause problems:

This is test & example mail.
This mail includes '&' sign.
!@#$%^&*()_+

With other words, when '&' is present it causes the error. I tried to replace the '&' with some example text and it does not work although for example replacing 't' with 'R' works perfectly and the message gets through with the replacement.
Reply
  #13  
Old August 18th, 2008, 03:05 PM
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 769
Default

Quote:
Originally Posted by zamuel
Well, as an example any of these strings will cause problems:

This is test & example mail.
This mail includes '&' sign.
!@#$%^&*()_+

With other words, when '&' is present it causes the error. I tried to replace the '&' with some example text and it does not work although for example replacing 't' with 'R' works perfectly and the message gets through with the replacement.
I have run a simple test using the string you supplied and the str_replace function.

Code:
[PHP]
$lcVar = "This is test & example mail.
This mail includes '&' sign.
!@#$%^&*()_+" ;
echo $lcVar . '<br /><br />' ;
$lcVar2 = str_replace('&', '_and_', $lcVar) ;
echo $lcVar2 . '<br /><br />' ;
[/PHP]
Output:
Expand|Select|Wrap|Line Numbers
  1. This is test & example mail. This mail includes '&' sign. !@#$%^&*()_+
  2.  
  3. This is test _and_ example mail. This mail includes '_and_' sign. !@#$%^_and_*()_+
  4.  
The str_replace there works fine.

Cheers
nathj
Reply
  #14  
Old August 18th, 2008, 03:07 PM
Member
 
Join Date: Sep 2006
Posts: 42
Default

XML_unserialize() is basically taking the XML string and making it to be object, which in this case can be used as $vars['root']['opmerking'] for posting the value of 'opmerking' as the text field for e-mail message.
Reply
  #15  
Old August 18th, 2008, 03:14 PM
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Age: 24
Posts: 5,306
Default

You can echo the value, and the '&' character is properly escaped, but when you send the email, there is no message body; is that correct?
Reply
  #16  
Old August 18th, 2008, 03:22 PM
Member
 
Join Date: Sep 2006
Posts: 42
Default

That is correct! I am able to echo before sending, but the message body is empty every time.
Reply
  #17  
Old August 18th, 2008, 07:26 PM
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Age: 24
Posts: 5,306
Default

There's a couple of possibilities here. The first idea is that the command line arguments passed to sendmail aren't escaped, so the shell sees the ampersand as the "run in background" operator instead of part of the message. That's not a good thing.

If the email breaks when the body contains quotes, a pipe ("|") or greater than as well, that's a good indication of what's going on.

The other alternative is that sendmail itself is doing something wrong with the ampersand.

Ultimately, my recommendation would be to use a class such as PHPMailer or Swift Mailer instead of relying on sendmail. Both PHPMailer and Swift Mailer implement SMTP independently of sendmail for better compatibility.
Reply
  #18  
Old August 19th, 2008, 07:57 AM
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 769
Default

Quote:
Originally Posted by pbmods
There's a couple of possibilities here. The first idea is that the command line arguments passed to sendmail aren't escaped, so the shell sees the ampersand as the "run in background" operator instead of part of the message. That's not a good thing.

If the email breaks when the body contains quotes, a pipe ("|") or greater than as well, that's a good indication of what's going on.
Well I've learnt something there. This is what I was trying to get at but didn't know for sure, so with your input I have discovered something new.

I love this forum.
nathj
Reply
  #19  
Old August 19th, 2008, 09:39 AM
Member
 
Join Date: Sep 2006
Posts: 42
Default

Ok, I have discovered few other errors. Additionally sign '<' also causes same behavior. Also when I add " or ' the massage becomes \' or \". I am starting to suspect the XML part for the source of the error.
Reply
  #20  
Old August 19th, 2008, 11:28 PM
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Age: 24
Posts: 5,306
Default

Does '>' do anything untoward as well, or is it just '<' and '&'?

Check your Apache error log and see if anything fishy is going on.
Reply
  #21  
Old August 20th, 2008, 10:36 AM
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 769
Default

Quote:
Originally Posted by zamuel
Ok, I have discovered few other errors. Additionally sign '<' also causes same behavior. Also when I add " or ' the massage becomes \' or \". I am starting to suspect the XML part for the source of the error.
The presence of \ is simpy mail() escaping the quotes. If you want rid of those simply use stripslashes() and that will sort you out.

cheers
nathj
Reply
  #22  
Old August 21st, 2008, 12:40 PM
Member
 
Join Date: Sep 2006
Posts: 42
Default

So, I decided to replace those problematic signs (<, & ). Couldn't come up with other solution. And nathj, thanks for the tip for stripslashes() function.

Thanks for all the help on this case.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles