2012-10-08 86 views
11

我試圖發送電子郵件在文本和HTML,但我不能正確地發送正確的標題。特別是,我想設置Content-Type標題,但我找不到如何分別爲html和文本部分設置它。梨郵件,如何在UTF-8發送純文本/文本+文本/ html

這是我的代碼:

$headers = array(
    'From'   => '[email protected]', 
    'Return-Path' => '[email protected]', 
    'Subject'  => 'mysubject', 
    'text_encoding' => '7bit', 
    'text_charset' => 'UTF-8', 
    'html_charset' => 'UTF-8', 
    'head_charset' => 'UTF-8', 
    'Content-Type' => 'text/html; charset=UTF-8' 
); 

$mime = new Mail_mime(); 

$html = '<html><body><b>my body</b></body></html>'; 
$text = 'my body'; 

$mime->setTXTBody($text); 
$mime->setHTMLBody($html); 

$body = $mime->get(); 
$headers = $mime->headers($headers); 
$mail_object =& Mail::factory('smtp', $GLOBALS['pear_mail_config']); 
$mail_object->send('[email protected]', $headers, $body); 

這就是電子郵件,我得到:

From: [email protected] 
Subject: mysubject 
text_encoding: 7bit 
text_charset: UTF-8 
html_charset: UTF-8 
head_charset: UTF-8 
Content-Type: multipart/alternative; 
    boundary="=_7adf2d854b1ad792c802a9db31084520" 
Message-Id: <.....cut.....> 
Date: Mon, 8 Oct 2012 15:40:54 +0200 (CEST) 
To: undisclosed-recipients:; 

--=_7adf2d854b1ad792c802a9db31084520 
Content-Transfer-Encoding: 7bit 
Content-Type: text/plain; charset="ISO-8859-1" 

my body 

--=_7adf2d854b1ad792c802a9db31084520 
Content-Transfer-Encoding: quoted-printable 
Content-Type: text/html; charset="ISO-8859-1" 

<html><body><b>my body</b></body></html> 
--=_7adf2d854b1ad792c802a9db31084520-- 

看來,Content-Type頭我設置完全忽略。我曾預料過一些setHTMLHeaders和setTXTHeaders函數,但似乎沒有這樣的東西。我錯過了什麼嗎?我如何將兩個Content-Type標頭設置爲UTF-8?

回答

31

我發現頭文件應該寫得不同。特別是,其中一些是mime對象的參數,而不是電子郵件標題。然後mime_params數組應該被傳遞給get()函數。

這是設置頁眉的正確方法:

$headers = array(
    'From'   => '[email protected]', 
    'Return-Path' => '[email protected]', 
    'Subject'  => 'mysubject', 
    'Content-Type' => 'text/html; charset=UTF-8' 
); 

$mime_params = array(
    'text_encoding' => '7bit', 
    'text_charset' => 'UTF-8', 
    'html_charset' => 'UTF-8', 
    'head_charset' => 'UTF-8' 
); 

$mime = new Mail_mime(); 

$html = '<html><body><b>my body</b></body></html>'; 
$text = 'my body'; 

$mime->setTXTBody($text); 
$mime->setHTMLBody($html); 

$body = $mime->get($mime_params); 
$headers = $mime->headers($headers); 
$mail_object =& Mail::factory('smtp', $GLOBALS['pear_mail_config']); 
$mail_object->send('[email protected]', $headers, $body); 
+0

記住要加上'require_once '郵件/ mime.php';'。 – Knu

+1

最後,一個答案... –

+1

絕對的編碼類型應該通過Mime-> get()。投票。 –

相關問題