I use Postfix as a mailserver. I have Ubuntu OS.
Then I use PHP to send emails.
Problem is that none of my emails are encoded properly by a mailsoftware which my VPS provider uses.
According to them, the problem lies with me.
It is only the name field which isn't encoded properly. For example "Björn" becomes "Björn" in my emails.
However, when I echo the $name
, it outputs "Björn" which is correct.
Also, gmail and hotmail does show it correctly.
The strange part is that the "text" (the message itself) is encoded properly.
I use the following for sending mail:
$headers="MIME-Version: 1.0"."\n";
$headers.="Content-type: text/plain; charset=UTF-8"."\n";
$headers.="From: $name <$email>"."\n";
$name= iconv(mb_detect_encoding($name), "UTF-8//IGNORE//TRANSLIT", $name);
//// I HAVE TRIED WITH AND WITHOUT THE LINE ABOVE, NO DIFFERENCE
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $text, $headers, '[email protected]');
I have tried with and without the iconv line also, no luck.
The last thing I can think of is POSTFIX, could there be a setting for character encoding there? Anybody knows?
The message text can be any character set you define (because you define the character set for the mail in the mail header (content-type = utf8), but the headers (subject, from - name, email) are defined in the client that is reading the email, so you can't affect that. Gmail is using utf8 as the default, while other clients that are viewing the emails can use anything they like and basically only ASCII characters are fully supported everywhere.
Edit: You can have characters outside US-ASCII in the message header if they are MIME encoded ("=?charset?encoding?encoded text?=")
One problem I see is that you've converted
$name
to UTF-8 after embedding it in the$headers
. So what's getting sent to postfix is whatever was in there before. You shouldiconv()
it and add the MIME encoding to it before adding it into$headers
.