 | 
August 18th, 2008, 10:39 AM
| | Member | | Join Date: Sep 2006
Posts: 42
| | 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!
| 
August 18th, 2008, 10:54 AM
|  | Expert | | Join Date: May 2007 Location: North Tyneside
Posts: 769
| | 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
| 
August 18th, 2008, 11:10 AM
| | Member | | Join Date: Sep 2006
Posts: 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. -
-
$city = $_GET['city'];
-
-
$user = new user();
-
$address = $user->SQL->query("select email FROM tblRegios WHERE plaats='".$city."'");
-
-
if (sizeof($result = $user->SQL->result())) {
-
foreach($result as $key => $val)
-
{
-
if(isset($val['email']))
-
$email = $val['email'];
-
}
-
}
-
-
$to = $email;
-
$subject = 'Mail';
-
-
$headers = 'From';
-
$vars = XML_unserialize($_POST['xml']);
-
-
mail($to, $subject, $vars['root']['opmerking'], $headers);
-
So, what I want to do is replace '&' with something before sending and then decode it to be '&' again when it is being read.
| 
August 18th, 2008, 11:15 AM
|  | Expert | | Join Date: May 2007 Location: North Tyneside
Posts: 769
| |
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
| 
August 18th, 2008, 11:40 AM
| | Member | | Join Date: Sep 2006
Posts: 42
| |
Yes, simply part of the message. I would like the message keep the same form when received, so & should not be 'and'.
| 
August 18th, 2008, 12:00 PM
|  | Expert | | Join Date: May 2007 Location: North Tyneside
Posts: 769
| | 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
| 
August 18th, 2008, 12:22 PM
|  | Moderator | | Join Date: Jun 2007 Location: York England :) Age: 18
Posts: 2,831
| | 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.
| 
August 18th, 2008, 12:54 PM
| | Member | | Join Date: Sep 2006
Posts: 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.
| 
August 18th, 2008, 12:56 PM
|  | Expert | | Join Date: May 2007 Location: North Tyneside
Posts: 769
| |
Hi,
Could you maybe post the message text? There maybe something in there that is causing the issue.
Cheers
nathj
| 
August 18th, 2008, 01:03 PM
| | Member | | Join Date: Sep 2006
Posts: 42
| |
Well, the message text can be what ever user chooses to type in, but the text area itself is assigned through line -
$page->assign("textOpmerking",$form->textarea("opmerking","",40,15));
-
and the text are itself is defined in different file like: -
function textarea($name,$value="",$cols=40,$rows=4,$js = false,$class = false) {
-
$id = $name;
-
$array = array("name","type","cols","rows","class","id");
-
foreach($array as $a) $return .= ($$a!==false) ? " $a=\"".$$a."\"" : "";
-
return $this->display($name,"textarea","<textarea ".$return." $js>".((isset($this->values[$name])) ? $this->values[$name] : $value)."</textarea>");
-
}
-
| 
August 18th, 2008, 02:39 PM
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas Age: 24
Posts: 5,306
| |
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?
| 
August 18th, 2008, 02:54 PM
| | Member | | Join Date: Sep 2006
Posts: 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.
| 
August 18th, 2008, 03:05 PM
|  | Expert | | Join Date: May 2007 Location: North Tyneside
Posts: 769
| | 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: -
This is test & example mail. This mail includes '&' sign. !@#$%^&*()_+
-
-
This is test _and_ example mail. This mail includes '_and_' sign. !@#$%^_and_*()_+
-
The str_replace there works fine.
Cheers
nathj
| 
August 18th, 2008, 03:07 PM
| | Member | | Join Date: Sep 2006
Posts: 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.
| 
August 18th, 2008, 03:14 PM
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas Age: 24
Posts: 5,306
| |
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?
| 
August 18th, 2008, 03:22 PM
| | Member | | Join Date: Sep 2006
Posts: 42
| |
That is correct! I am able to echo before sending, but the message body is empty every time.
| 
August 18th, 2008, 07:26 PM
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas Age: 24
Posts: 5,306
| |
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.
| 
August 19th, 2008, 07:57 AM
|  | Expert | | Join Date: May 2007 Location: North Tyneside
Posts: 769
| | 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
| 
August 19th, 2008, 09:39 AM
| | Member | | Join Date: Sep 2006
Posts: 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.
| 
August 19th, 2008, 11:28 PM
|  | Site Moderator | | Join Date: Apr 2007 Location: Texas Age: 24
Posts: 5,306
| |
Does '>' do anything untoward as well, or is it just '<' and '&'?
Check your Apache error log and see if anything fishy is going on.
| 
August 20th, 2008, 10:36 AM
|  | Expert | | Join Date: May 2007 Location: North Tyneside
Posts: 769
| | 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
| 
August 21st, 2008, 12:40 PM
| | Member | | Join Date: Sep 2006
Posts: 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.
|  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | 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.
|