我正在为我的一个应用程序实现多语言支持。我正在尝试使用PHP Pear::Mail和Pear::Mail_Mime包发送多语言邮件。
以下是我的邮件发送功能。
function _send($to, $subject, $content){
$smtp_details = $this->settings->getSMTPDetails();
$host = $smtp_details['smtp_host'];
$port = $smtp_details['smtp_port'];
$username = $smtp_details['smtp_username'];
$password = $smtp_details['smtp_password'];
$from_address = $smtp_details['smtp_email'];
/*
* Decode the Unicode string and encrypt the string
*/
$subject = base64_encode($subject);
$content = base64_encode($content);
if ($this->settings->getLanguage() != LANGUAGE_ENGLISH){
$subject = '=?UTF-8?B?'.$subject.'?=';
}
$headers = array ('From' => $from_address,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; multipart/related; boundary=DeS-mixed-{$_boundary}; charset=UTF-8n',
'Content-Transfer-Encoding' => 'BASE64',
'MIME-Version' => '1.0' );
// Creating the Mime message
$mime = new Mail_mime("n");
// Setting the body of the email
$mime->setHTMLBody($content);
$body = $mime->get();
//$header = $mime->headers($headers);
$smtp = Mail::factory('smtp',array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$connectionCount = 0;
do {
$mail = $smtp->send($to, $headers, $body);
$connectionCount++;
if (PEAR::isError($mail) && $connectionCount < 3) {
//echo 'Retrying to send email using Host:'.$host;
sleep(1);
} else {
if (PEAR::isError($mail)){
throw new Exception('Mail sending error: '.$mail->getMessage());
}else{
return true;
}
}
}while (1);
}
调用函数为:
echo $this->_send($to, json_decode("u0B9Au0BC7u0BBEu0BA4u0BA9u0BC8 u0BAEu0BBFu0BA9u0BCDu0BA9u0B9Eu0BCDu0B9Au0BB2u0BCD"), json_decode("u0B87u0BA4u0BC1 u0B92u0BB0u0BC1 u0B9Au0BC7u0BBEu0BA4u0BA9u0BC8 u0BAEu0BBFu0BA9u0BCDu0BA9u0B9Eu0BCDu0B9Au0BB2u0BCD"));
注意:电子邮件内容是 Unicode JavaScript 转义字符串。
预期产出:
Subject: சோதனை மின்னஞ்சல்
Body: இது ஒரு சோதனை மின்னஞ்சல்
我得到的实际输出:
Subject: சோதனை மின்னஞ்சல்
Body: இந்த ஒரு சோதனை மின்�®
它被修剪为我收件箱中电子邮件正文内容的某些限制。我是否错过了任何标头配置?任何人遇到同样的问题,请分享您的解决方案。
提前谢谢。
您是否从数据库中获取数据? 您需要为电子邮件和数据库发送 UTF-8 编码你这个之前选择任何数据
mysql_query ("set character_set_client='utf8'"); mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");