remove odd chars from email form

4 posts by 2 authors in: Forums > CMS Builder
Last Post: March 8, 2010   (RSS)

By jarvis - March 5, 2010

Hi, Hope someone can help. I've got the following code:
// process form
if (@$_REQUEST['submitForm']) {

// error checking
$errorsAndAlerts = "";
if (!@$_REQUEST['name']) { $errorsAndAlerts = "Please fill out all fields\n"; }
if (!@$_REQUEST['email']) { $errorsAndAlerts = "Please fill out all fields\n"; }
else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAndAlerts = "That email address is not valid\n"; }
if (!@$_REQUEST['enquiry']) { $errorsAndAlerts = "Please fill out all fields\n"; }

// send email user
if (!$errorsAndAlerts) {
$from = $_REQUEST['email'];
$to = "info@domain.com";
$subject = "Quick Contact";
$message = <<<__TEXT__

Full name: {$_REQUEST['name']}
Email: {$_REQUEST['email']}
Enquiry: {$_REQUEST['enquiry']}


But it gives the following in the reply
I want to appoint you at £40,000per month

I tried htmlspecialchars but maybe I used it incorrectly as couldn't get the issue resolved.

Any help would be much appreciated.

Many thanks

Re: [jarvis] remove odd chars from email form

By jarvis - March 8, 2010

Solved using $textToConvert = $_REQUEST['enquiry'];
$convertedText = iconv("UTF-8", "ISO-8859-1", $textToConvert);

Re: [jarvis] remove odd chars from email form

By Dave - March 8, 2010

Hi Jarvis,

Nice work, glad you got it working! :)

I wouldn't change it if it's working, but another way to do it is to set the charset for the email to be utf-8 as well. Here's the code we do that with for the CMSB password reminder:

$headers = "Content-Type: text/plain; charset=UTF-8\n";
$headers .= "From: {$SETTINGS['adminEmail']}\n";
$mailResult = @mail($to, $subject, $message, $headers);


Alternatively, we have a new mail function we're working on in the latest version which is at the bottom of /lib/common.php and works like this:

sendMessage(array(
'from' => "from@example.com",
'to' => "to@example.com",
'subject' => "Enter subject here, supports utf-8 content",
'text' => "Text message content",
));


It automatically encodes content as utf-8, supports text and/or html messages, and even file attachments.

If it's working now I'd just leave it as-is, but if you run into more troubles in future you might give that a try. Just called sendMessage with the above code rather than mail.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com