我在使用波斯语发送电子邮件时遇到问题。在gmail上没问题,所有文字都很好。但在像雅虎,cpanel webmail等的订单中我得到了未知的字符。我该怎么做才能解决这个问题?
这是我的代码:
<?php
function emailHtml($from, $subject, $message, $to) {
require_once "Mail.php";
$headers = array ('MIME-Version' => "1.0", 'Content-type' => "text/html; charset=utf-8;", 'From' => $from, 'To' => $to, 'Subject' => $subject);
$m = Mail::factory('mail');
$mail = $m->send($to, $headers, $message);
if (PEAR::isError($mail)){
return 0;
}else{
return 1;
}
}
?>
我正在使用PEAR邮件发送电子邮件。
答案 0 :(得分:2)
您需要实例化Mail_Mime
,设置标题和正文HTML,从您的mime实例中检索它们并将它们传递给您的Mail实例。引用文档中的example:
<?php
include('Mail.php');
include('Mail/mime.php');
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = '/home/richard/example.php';
$crlf = "\n";
$hdrs = array(
'From' => 'you@yourdomain.com',
'Subject' => 'Test mime message',
'Content-Type' => 'text/html; charset="UTF-8"'
);
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
//do not ever try to call these lines in reverse order
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
$mail->send('postmaster@localhost', $hdrs, $body);
?>
我编辑了上面的文档示例以包含Content-Type标头。如果客户端不支持HTML,建议您以纯文本和HTML格式提供邮件正文。此外,您不需要与添加附件相关的部分,但我出于知识的缘故将它们留下。