Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with '&' charachter.

Member
 
Join Date: Sep 2006
Posts: 42
#1: Aug 18 '08
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!
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#2: Aug 18 '08

re: Problem with '&' charachter.


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
Member
 
Join Date: Sep 2006
Posts: 42
#3: Aug 18 '08

re: Problem with '&' charachter.


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.
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#4: Aug 18 '08

re: Problem with '&' charachter.


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
Member
 
Join Date: Sep 2006
Posts: 42
#5: Aug 18 '08

re: Problem with '&' charachter.


Yes, simply part of the message. I would like the message keep the same form when received, so & should not be 'and'.
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#6: Aug 18 '08

re: Problem with '&' charachter.


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
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#7: Aug 18 '08

re: Problem with '&' charachter.


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.
Member
 
Join Date: Sep 2006
Posts: 42
#8: Aug 18 '08

re: Problem with '&' charachter.


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.
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#9: Aug 18 '08

re: Problem with '&' charachter.


Hi,

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

Cheers
nathj
Member
 
Join Date: Sep 2006
Posts: 42
#10: Aug 18 '08

re: Problem with '&' charachter.


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.  
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#11: Aug 18 '08

re: Problem with '&' charachter.


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?
Member
 
Join Date: Sep 2006
Posts: 42
#12: Aug 18 '08

re: Problem with '&' charachter.


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.
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#13: Aug 18 '08

re: Problem with '&' charachter.


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
Member
 
Join Date: Sep 2006
Posts: 42
#14: Aug 18 '08

re: Problem with '&' charachter.


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.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#15: Aug 18 '08

re: Problem with '&' charachter.


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?
Member
 
Join Date: Sep 2006
Posts: 42
#16: Aug 18 '08

re: Problem with '&' charachter.


That is correct! I am able to echo before sending, but the message body is empty every time.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#17: Aug 18 '08

re: Problem with '&' charachter.


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.
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#18: Aug 19 '08

re: Problem with '&' charachter.


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
Member
 
Join Date: Sep 2006
Posts: 42
#19: Aug 19 '08

re: Problem with '&' charachter.


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.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#20: Aug 19 '08

re: Problem with '&' charachter.


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

Check your Apache error log and see if anything fishy is going on.
nathj's Avatar
Expert
 
Join Date: May 2007
Location: North Tyneside
Posts: 854
#21: Aug 20 '08

re: Problem with '&' charachter.


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
Member
 
Join Date: Sep 2006
Posts: 42
#22: Aug 21 '08

re: Problem with '&' charachter.


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