Pear Mail,如何以 UTF-8 格式发送纯文本/文本 + 文本/html [英] Pear Mail, how to send plain/text + text/html in UTF-8
问题描述
我正在尝试以文本和 html 格式发送电子邮件,但我无法正确发送正确的标题.特别是,我想设置 Content-Type 标头,但我找不到如何为 html 和文本部分单独设置它.
I'm trying to send an email in both text and html, but I can't correctly send the right headers. In particular, I'd like to set the Content-Type header, but I can't find how to set it separately for html and text parts.
这是我的代码:
$headers = array(
'From' => 'info@mydomain.com',
'Return-Path' => 'info@mydomain.com',
'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('test@mydomain.com', $headers, $body);
这是我收到的电子邮件:
That's the email I get:
From: info@mydomain.com
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?
It seems that the Content-Type header I set is totally ignored. I'd have expected some setHTMLHeaders and setTXTHeaders functions, but it seems like there's nothing like this. Am I missing something? How can I set both Content-Type headers to UTF-8?
推荐答案
我发现标题应该以不同的方式编写.特别是,其中一些是 mime 对象的参数,而不是电子邮件标题.然后应该将 mime_params 数组传递给 get() 函数.
I discovered that the headers are supposed to be written differently. In particular, some of them are parameters for the mime object, and not email headers. Then the mime_params array should be passed to the get() function.
这是设置标题的正确方法:
This is the correct way to set the headers:
$headers = array(
'From' => 'info@mydomain.com',
'Return-Path' => 'info@mydomain.com',
'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('test@mydomain.com', $headers, $body);
这篇关于Pear Mail,如何以 UTF-8 格式发送纯文本/文本 + 文本/html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!