2012-10-08 11 views
11

Sto tentando di inviare un'email sia in testo che in html, ma non riesco a inviare correttamente le intestazioni corrette. In particolare, vorrei impostare l'intestazione Content-Type, ma non riesco a trovare come impostarla separatamente per le parti html e text.Pera posta, come inviare plain/text + text/html in UTF-8

Questo è il mio codice:

$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); 

Questo è l'e-mail ottengo:

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-- 

Sembra che l'header Content-Type ho impostato è totalmente ignorata. Mi sarei aspettato alcune funzioni setHTMLHeaders e setTXTHeaders, ma sembra che non ci sia nulla di simile. Mi sto perdendo qualcosa? Come posso impostare entrambe le intestazioni Content-Type su UTF-8?

risposta

31

Ho scoperto che le intestazioni dovrebbero essere scritte in modo diverso. In particolare, alcuni di essi sono parametri per l'oggetto mime e non intestazioni di email. Quindi l'array mime_params deve essere passato alla funzione get().

Questo è il modo corretto di impostare le intestazioni:

$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

ricordarsi di aggiungere 'require_once 'Mail/mime.php';'. – Knu

+1

Infine, una risposta ... –

+1

sicuramente il tipo di codifica dovrebbe essere passato attraverso il Mime-> get(). voto. –