Email generation from contact from - special characters not working

I’m a relative newbee to php but have implemented a simple contact form and php script to collect data and send an email. The code I’m using is below: (this is based on an old tutorial on another site - tutvid)

[php]/* Subject and Email variable */

$emailSubject = $_POST['subject'];
$webMaster = '[email protected]';

/* Gathering data variables */

$nameField = $_POST['name'];
$emailField = $_POST['email'];
$phoneField = $_POST['phone'];
$addressField = $_POST['address'];
$commentField = $_POST['comment'];
$mailinglistField = $_POST['mailinglist'];

$body = <<<EOD

Enquiry sent via domain.com contact form


From: $nameField
Email: $emailField
Phone: $phoneField
Address: $addressField
Mailing list: $mailinglistField

$commentField
EOD;
$headers = "From: $emailField\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

// Change to the URL you want to redirect to
$URL=“http://www.domain.com/contact.php?thank=you”;

header (“Location: $URL”); [/php]

My issue is hopefully a simple one to correct, but I wasn’t sure where to start. When someone fills the form out, sends it and has used characters like ’ - the resulting email displays the character as ’

How do I get the code to accept these characters? or is my problem on the form page itself?

Any help would be much appreciated. Thanks.

Is get_magic_quotes_gpc() returning true? If yes, congratulations, you’ve got magic quotes enabled, and you may need to strip them from your variables.

Magic quotes is an old (deprecated in 5.3, removed in 5.4) PHP “safety” measure - it adds a backslash in front of ’ and " in user inputs. You can disable it, or you can strip the quotes after they’ve been put. Or you can just hop on to PHP 5.3 or 5.4 and not have to deal with such things.

Thanks for the reply. Some research later and it seems that my hosting package is already running php 5.3.24.

Should it still be doing this? And how would I disable “magic quotes”? or strip them from my variables? Please forgive my lack of php knowledge. Cheers.

Try the following:

[php]if (get_magic_quotes_gpc()) echo “MAgic quotes are on”;[/php]

If you see the echo, you’ve got magic quotes on (PHP5.3 deprecates them - they’re still there, but they’re scheduled for removal). So if they’re there, that’s it.

The usual fix is the following:
[php]if (!get_magic_quotes_gpc()) {
function my_escape(&$value, $key) {$value = mysql_real_escape_string($value);}
$gpc = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
array_walk_recursive($gpc, ‘my_escape’);
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service