I have a problem with sending mails that preserve accented characters. By reading some pages and the answers to previously asked questions I was able to find a solution that works (partially).
I have no problem with the following code, that works correctly (i.e. I see accented characters in the eMail client I use). It uses the PEAR php library. However, this code is only good for English, as I will explain later on.
...
$headers['From'] = "info@website.it";
$headers['Subject'] = 'Request';
$headers['Reply-To'] = $sanitized_email;
$headers["Content-Type"] = 'text/html; charset=UTF-8';
$headers["Content-Transfer-Encoding"]= "8bit";
$host= _CMS_MAIL_SERVER;
$username = "info@website.it";
$password = "pass";
$crlf = "\n";
$mime = new Mail_mime(array('eol' => $crlf));
$html_body = ...;
// read $html_body from file
// substitute some %tags% in $html_body
$mime->setHTMLBody($html_body);
$mimeparams['text_encoding']="8bit";
$mimeparams['text_charset']="UTF-8";
$mimeparams['html_charset']="UTF-8";
$mimeparams['head_charset']="UTF-8";
$mimebody = $mime->get($mimeparams);
$hdrs = $mime->headers($headers);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail_res = $smtp->send($recipients, $hdrs, $mimebody);
...
In the $html_body I have some tags that I need to substitute with time and date values, as returned by date or strftime(..). Since the default locale is English, in order to have Italian dates and times I need to set the locale before calling strftime(…).
If I add this line:
setlocale(LC_TIME, "it_IT");
accented characters are not displayed correctly by the mail client. More in details: accented characters returned by strftime are displayed correctly (e.g. lunedì), while accented characters submitted via the form field are not.
I made some research to understand how I can correct that error, but I did not find any solution. Furthermore, it is not clear to me what the relationship between setlocale on LC_TIME and accented characters is.
Is there anyone who can help me?
I solved the problem.
setlocale(…) switches character encoding even if it does not need to.
In this case setlocale(LC_TIME, “it_IT”) was switching from UTF-8 to ISO-8859-1 and the receiving page was using UTF-8.
When using UTF-8 the correct way to set locale is:
setlocale(LC_TIME, “it_IT.UTF-8”).
It was obvious the problem was related to character encodings, but setlocale should not change locale if the encoding in use is ok for the language (or should it?).