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);
}
Author by
deepWebMie
Updated on July 08, 2022Comments
-
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 over 12 yearshere 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 almost 10 yearsUTF-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 about 9 yearsWhich email tools stil use 7-bit transfer encoding ?
-
Vladimir Panteleev over 8 yearsThe 4th parameter to php
mail()
is a string, not an array!! -
Mel_T over 8 yearsBe sure to write
charset=UTF-8
without quotes aroundUTF-8
! -
Stewart over 7 yearsOnly use
text/html
if you're sending an email in HTML format. For plain text emails, you wanttext/plain
. -
David42 over 7 yearsI think you mean "to your message" rather than "to your message body".