2009-11-21 23 views
0

我試圖使用PHP mail函數發送非英文字符的文本電子郵件。但相反,我的消息帶着看起來很有趣的垃圾角色。我如何解決它?爲什麼在使用非英文字符發送電子郵件時,某些字符顯示爲問號?

我使用這段代碼:

function _mail($to, $subject, $content) 
{ 

$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

mail($to, $subject, $content, $headers); 
} 

某些字符來如問號...

+1

請告訴我們你試過了什麼?還有一個小測試消息的結果,包括它的頭文件? (如果你不知道什麼,請參閱http://superuser.com/questions/66082/why-do-russian-characters-in-some-received-emails-change-when-reading-in-david/66104#66104我的意思是。) – Arjan 2009-11-21 10:18:13

+0

[什麼是字符編碼,爲什麼我應該打擾它]可能的重複(http://stackoverflow.com/questions/10611455/what-is-character-encoding-and-why-should-i-打擾與它) – Raedwald 2015-04-10 12:37:30

回答

2

的關鍵是使用UTF-8字符集。

添加Content-Type: text/html; charset=UTF-8MIME-Version 1.0Content-Transfer-Encoding: quoted-printable到郵件頭,像這樣:

$headers = 'From: [email protected]' . "\r\n" . 
      'Reply-To: [email protected]' . "\r\n" . 
      'Content-Type: text/html; charset=UTF-8' . "\r\n" . 
      'MIME-Version: 1.0' . "\r\n" . 
      'Content-Transfer-Encoding: quoted-printable' . "\r\n" . 
      'X-Mailer: PHP/' . phpversion(); // Why would you want to send this header? 

如果你會使用HTML而不是文本,你想也需要一個META標記添加到的HEAD你(X)HTML郵件:

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

你還需要'MIME-Version:1.0'和'Content-Transfer-Encoding:quoted-printable'正文,因爲UTF-8字符通常不會完整地通過郵件系統。 – bobince 2009-11-21 13:12:54

+0

謝謝,bobince!我更新了我的帖子。 – 2009-11-21 14:20:54

+0

但顯然,當使用quoted-printable時,那麼這個消息應該編碼得很好。 – Arjan 2009-11-21 15:25:27

3

郵件封裝器如Swiftmailer可能會對您有所幫助。

相關問題