By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

Problem with '&' charachter.

P: 42
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!
Aug 18 '08 #1
Share this Question
Share on Google+
21 Replies


nathj
Expert 100+
P: 938
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
Aug 18 '08 #2

P: 42
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.
Aug 18 '08 #3

nathj
Expert 100+
P: 938
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
Aug 18 '08 #4

P: 42
Yes, simply part of the message. I would like the message keep the same form when received, so & should not be 'and'.
Aug 18 '08 #5

nathj
Expert 100+
P: 938
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
Aug 18 '08 #6

Markus
Expert 5K+
P: 6,050
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.
Aug 18 '08 #7

P: 42
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.
Aug 18 '08 #8

nathj
Expert 100+
P: 938
Hi,

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

Cheers
nathj
Aug 18 '08 #9

P: 42
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.  
Aug 18 '08 #10

pbmods
Expert 5K+
P: 5,821
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?
Aug 18 '08 #11

P: 42
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.
Aug 18 '08 #12

nathj
Expert 100+
P: 938
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
Aug 18 '08 #13

P: 42
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.
Aug 18 '08 #14

pbmods
Expert 5K+
P: 5,821
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?
Aug 18 '08 #15

P: 42
That is correct! I am able to echo before sending, but the message body is empty every time.
Aug 18 '08 #16

pbmods
Expert 5K+
P: 5,821
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.
Aug 18 '08 #17

nathj
Expert 100+
P: 938
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
Aug 19 '08 #18

P: 42
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.
Aug 19 '08 #19

pbmods
Expert 5K+
P: 5,821
Does '>' do anything untoward as well, or is it just '<' and '&'?

Check your Apache error log and see if anything fishy is going on.
Aug 19 '08 #20

nathj
Expert 100+
P: 938
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
Aug 20 '08 #21

P: 42
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.
Aug 21 '08 #22

Post your reply

Sign in to post your reply or Sign up for a free account.