How to send UTF-8 email?

219,659

Solution 1

You can add header "Content-Type: text/html; charset=UTF-8" to your message body.

$headers = "Content-Type: text/html; charset=UTF-8";

If you use native mail() function $headers array will be the 4th parameter mail($to, $subject, $message, $headers)

If you user PEAR Mail::factory() code will be:

$smtp = Mail::factory('smtp', $params);

$mail = $smtp->send($to, $headers, $body);

Solution 2

If not HTML, then UTF-8 is not recommended. koi8-r and windows-1251 only without problems. So use html mail.

$headers['Content-Type']='text/html; charset=UTF-8';
$body='<html><head><meta charset="UTF-8"><title>ESP Notufy - ESP Сообщения</title></head><body>'.$text.'</body></html>';


$mail_object=& Mail::factory('smtp',
    array ('host' => $host,
        'auth' => true,
        'username' => $username,
        'password' => $password));
$mail_object->send($recipents, $headers, $body);
}
Share:
219,659
deepWebMie
Author by

deepWebMie

Updated on July 08, 2022

Comments

  • deepWebMie
    deepWebMie almost 2 years

    When I send out the email, the email does not show characters other than english. It does show like below:

    余生ä»ä»

    May know actually what cause this? Even I tried to added Content-type and charset in the script, it still show the same.

    I used Mail::Factory("mail");

  • deepWebMie
    deepWebMie over 12 years
    here is my code $mail = Mail::factory("mail"); $to = "[email protected]"; $body = "好信"; $headers = array("Content-type =>"text/plain: charset=\"UTF-8","From" => "[email protected]","To" => "[email protected]","Subject" => "Test Send Chinese mail-好信"); $mail->send($to, $headers, $body) Is this correct? It does not show what I expect. It should display out chinese word in body,header or even subject.
  • ClearCrescendo
    ClearCrescendo almost 10 years
    UTF-8 requires 8 bits. Most email tools still use: Content-Transfer-Encoding: 7bit. The results will be dependent on the tools your content goes through but try adding an entry to the headers array of Content-Transfer-Encoding: 8bit
  • Grodriguez
    Grodriguez about 9 years
    Which email tools stil use 7-bit transfer encoding ?
  • Vladimir Panteleev
    Vladimir Panteleev over 8 years
    The 4th parameter to php mail() is a string, not an array!!
  • Mel_T
    Mel_T over 8 years
    Be sure to write charset=UTF-8 without quotes around UTF-8!
  • Stewart
    Stewart over 7 years
    Only use text/html if you're sending an email in HTML format. For plain text emails, you want text/plain.
  • David42
    David42 over 7 years
    I think you mean "to your message" rather than "to your message body".