General Question

Evan's avatar

Text within a PHP Mail function...?

Asked by Evan (805points) August 6th, 2007

I'm using the PHP Mail() function, and i'm using a standard html form to have the info put in, and then defining each field as part of

$mail_body = "example: $example\n\nExample on line 2: $example2";

and so on.. the problem i'm having is that within the mult-line < textarea > field, where if a contraction is typed in (such as can't, or don't), then in the email it shows up as "can\'t" or "don\'t"

why is this, and how do i fix it? please please? :) :)

Observing members: 0 Composing members: 0

5 Answers

sferik's avatar

I believe the backslashes are being inserted by magic_quotes_gpc, which is on by default. You can check to see to see whether it's on or not by using the get_magic_quotes_gpc function. You can turn it off globally in the php.ini or use the stripslashes function like so:

$example = stripslashes($example);
$example2 = stripslashes($example2);
// Using "here document" syntax makes the code more readable
$mail_body = <<<BODY
example: $example

Example on line 2: $example2
BODY;

Evan's avatar

thanks!

i just used the stripslashes function, since it was a small bit of code, and that worked well enough, but that was perfect! thnx again,

evan

Vincentt's avatar

According to the PHP manual "here document" text:

[...]behaves just like a double-quoted string, without the double-quotes. [...]

So you would still need the \n .

Also, to be more host-independent, you could better use:

if(get_magic_quotes_gpc())
{
$example1 = stripslashes($example1);
$example2 = stripslashes($example2);
}

This way, if magic_quotes_gpc is turned off it won't remove slashes that are supposed to be there (which might present a security risk).

sferik's avatar

Vincentt, "here document" syntax does indeed behave like a double-quoted string, but since there are new lines inside the string, \n is not necessary. I also could have written it as a double-quoted string without the \n like so...

$mail_body = "example: $example

Example on line 2: $example2";

...but as I said in the comment, I find heredoc to be easier to read.

sferik's avatar

If you are going to be doing this more than once, you may want to create a function that looks like this...

function stripslashes_if_gpc_magic_quotes( $string ) {
if(get_magic_quotes_gpc()) {
return stripslashes($string);
} else {
return $string;
}
}

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther