Accented characters [from] field mail

I’ve made a flash mailform. This communicates with below script to send the mail. Which works ok. I’ve used utf8_decode to also make it send accented characters like ë á é ü etc. In the message field this works ok. All accented characters display. In the ‘from’ field however, these accented characters still appear funny. Like when it should display ‘René’ in the from-field, it displays ‘renX’ in the from-field. In the message field is still display’s René.

What do I have to do to also make it work in the from-field?

<?php
// initialize variables for To and Subject fields
$to = 'info@test.nl';
$subject = 'Een vraag';
$from = stripslashes(utf8_decode($_POST['from']));
$email = stripslashes(utf8_decode($_POST['email']));
$comments = stripslashes(utf8_decode($_POST['comments']));

// build message body from variables received in the POST array
$message = 'Van: ' . $from . "\
\
";
$message .= 'Email: '. $email ."\
\
";
$message .= 'Bericht: '. $comments;

//convert flash line breaks
$message = ereg_replace("\\r", "\
", $message);

// add additional email headers for more user-friendly reply
$additionalHeaders = "MIME-Version: 1.0\\r\
";
$additionalHeaders .= "Content-type: text/html; charset= ISO 8859-1\\r\
"; 
$additionalHeaders .= "From: " . $from ." <" . $email . ">\\r\
"; 
$additionalHeaders .= "Reply-To: " . $email;


// send email message
$OK = mail($to, $subject, $message, $additionalHeaders);
// let Flash know what the result was
if ($OK) {
  echo 'sent=OK';
  }
  else {
  echo 'sent=failed&reason='. urlencode('Er is een probleem met de server. Probeer het later nog eens.');
  }
?>

Character sets really isn’t something I’m strong on, however it may have something to do with the following line:

$additionalHeaders .= "Content-type: text/html; charset= ISO 8859-1\\r\
";

You’re telling the email client that the charset is Latin1 (ISO 8859-1), yet you are sending it as UTF-8.

Don’t think so? I had to use utf8_decode on the output from flash cause flash sends it’s output in utf8. Otherwise accented characters from the flash form would turn into strange codes. So that turns the utf8 output from flash into ISO 8859-1 for php. And in the message field of the email, these accented characters appear ok now. Accept in the from field of the email. René now turns into RenXX

I would suggest using the PHPMailer class as this will save you a lot of headaches:
http://phpmailer.worxware.com/

Once you get the class configured and running, it’s pretty trivial to get the character set and mime types you need.

-Bing

The Content-Type field only applies to the body of the message.

For the headers, you have to use MIME encoded-word:

There are quite a few PHP classes to handle emailing, such as PHPMailer or Swift Mailer, that will do all of this for you.

Does that mean I should also encode the mail fields like from: cc: bcc: by using mb_encode_mimeheader?

So like this?
$additionalHeaders .= “From: " . mb_encode_mimeheader($from, “iso-8859-1”, “Q”) .” <" . $email . ">\r
";

Yes.

I’ve tried putting it all in utf-8 now and used this mime encoding on the from field to turn that into utf-8 also.

However, when I send a mail as ‘René’, in the From field of the email I receive it says rené <test@mail.com>.

In the message text it reads:

From: rené

Email: test@mail.com

Message: rené is writing this email.

This is while the email is viewed in html mode. When I switch to plain text mode, the message text reads ‘René’ again, but the From field still ’ rené’.

How can I fix it that both the From field as the Message field in html mode both display accented characters?

<?php
// initialize variables for To and Subject fields
$to = 'info@testmail.nl';
$subject = 'Een testmail';
$from = $_POST["from"];
$email = $_POST["email"];
$comments = $_POST["comments"];

// build message body from variables received in the POST array
$message = "Van: $from \
\
";
$message .= "Email: $email \
\
";
$message .= "Bericht: $comments";
$message = stripslashes($message);

//convert flash line breaks
$message= str_replace("\\r", "\
", $message);
$message=nl2br($message);

// add additional email headers for more user-friendly reply
$additionalHeaders = "MIME-Version: 1.0\\r\
";
$additionalHeaders .= "Content-type: text/html; charset=utf-8\\r\
";
$additionalHeaders .= "From: " . mb_encode_mimeheader($from, "utf-8", "Q") ." <" . $email . ">\\r\
";
$additionalHeaders .= "Reply-To: ".$email."\\r\
";



// send email message
$OK = mail($to, $subject, $message, $additionalHeaders);
// let Flash know what the result was
if ($OK) {
  echo 'sent=OK';
  }
  else {
  echo 'sent=failed&reason='. urlencode('Er is een probleem met de server. Probeer het later nog eens.');
  }
?>