Search code examples
phpemailutf-8smtpencode

PHP, SMTP - Subject in email shows International Characters as HTML_Entities


I've spent over half a day on this - just trying to get the email Subject to appear normal. But international characters appear as HTML_Entities when the email lands in the inbox.

For example: Tydzień o Jedność

Shows up as: Tydzień o Jedność

I already encode the email Subject for UTF-8:

$this->subject = "=?UTF-8?B?" . base64_encode($this->subject) . "?=";

When I get the email (Gmail), the header appears as:

Subject: =?UTF-8?B?VHlkemllJiMzMjQ7IG8gSmVkbm8mIzM0NzsmIzI2Mzs=?=

So it seems to encode properly. But for some reason, Gmail still displays the Subject with HTML_Entities.

Any ideas?

See code: SMTP mail class


Solution

  • Is $this->subject a valid UTF-8 string? Are you working with UTF-8 encoded .php files (no BOM)?

    Also, try doing:

    $this->subject = "=?UTF-8?B?" . base64_encode(html_entity_decode($this->subject, ENT_COMPAT, 'UTF-8')) . "?=";