mistery_girl mistery_girl - 4 months ago 11
PHP Question

How to output Chinese in HTML file

I have a form and insert some chinese words in database and it's ok. Table charset is UTF8. Problem appears when I select this data and send it via mail as HTML attachment.
Then, Chinese doesn't display properly. How to fix charset before send data via mail? Should I use some headers and will it work?

My code looks like that:



//$attachedBodyContent is data from database that contains some chinese words
Mail::send(
"emails.applicationTemplate",
$data,
function($message) use ($data, $template, $subject, $attachedBodyContent) {
$message->to($data['email'], $data['name'])
->from($template['from'],$template['from_name'])
->subject($subject)
->attachData($attachedBodyContent,'YourApplicationData.html');

}
);




Answer

When you generate .html attach file you should include in your <head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

In this case you can use this code for merge your content with <head>

<?php
    $header = '<!DOCTYPE html>
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>';
    $footer = '</body>
    </html>';
    $allContent = $header.$attachedBodyContent.$footer;
?>