梨邮件,如何发送纯/文+ text / html的是UTF-8

问题描述 投票:12回答:1

我想在文本和HTML都发送电子邮件,但我不能正确地发送正确的头。特别是,我想设置Content-Type头,但我怎么也找不到单独设置为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?

php pear
1个回答
34
投票

我发现,头都应该是不同的写入。特别是,他们中的一些是为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);
© www.soinside.com 2019 - 2024. All rights reserved.