views:

312

answers:

4

Hi,

I am having some trouble with foreign characters when sending an e-mail. Could someone advise me on what to do. I think the problem could be one of three things.

1) The html page encoding is incorrect. (Would this affect the POST data from the form?) 2) The mail function doesn't have any encoding. Thus the program doesn't know how to read it. (Most likely) 3) The file itself doesn't have the right encoding and thus is making problems. (Probably quite unlikely)

Are there any other possible causes?

I am trying to knock these out 1 by 1 until I find the problem. I think that option 2 is the most likely cause. How do I add proper - universal encoding to a mail function?

This is what I have at the moment.

$mail_sent = mail($client_email, $title, $message, "From: {$visitor_email}");

I am currently aware that form does not send polish or Swedish characters.

I would be very grateful if someone could point out any other possible causes and tell me what encoding I need to use to send e-mails.

Thanks a lot.

+3  A: 

As far as I know PHP does not support UTF-8 as default encoding for its strings. You need to use the relevant encoding/handling functions for the encoding you would prefer.

Also add a 'Content-Type:text/html;charset=utf-8' to your email headers so the email clients will display the characters correctly (or replace with your encoding of choice).

Belrog
A: 

You didn’t specify the type and encoding of your content. Try this:

$headerFields = array(
    "From: {$visitor_email}",
    "MIME-Version: 1.0",
    "Content-Type: text/html;charset=utf-8"
);
$mail_sent = mail($client_email, $title, $message, implode("\r\n", $headerFields));
Gumbo
A: 

Use a PHP mail wrapper, it will save your sanity (speaking from experience here). For example, PHPMailer allows you to set outgoing e-mail encoding and builds the message for you.

Also, use UTF-8; it's almost universally supported nowadays and covers all the characters you would ever need.

Piskvor
A: 

I use the following code:

  $text = "";
  $text .= "<html>\n";
  $text .= "<body style=\"font-family:Arial; \">\n";

  $text .= "<b>Add Your text here . . .</b>";
  $text .= date("d.m.Y") . " <br><br>\n\n";

  $text .= "</body>\n";
  $text .= "</html>\n";

$headers = 'From: ' . $myEmail . "\r\n";
$headers .= 'To: ' . $depEmail . "\r\n";    
$headers .= 'Return-Path: ' . $myEmail . "\r\n";
$headers .= 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-Type: text/HTML; charset=ISO-8859-1' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n";
$headers .= $text . "\r\n";

set_time_limit(30);
if (!mail('', 'Demande information', '', $headers)) {
  Alert(...)
Atara