当字符是波斯语时,我从PHP向雅虎电子邮件发送电子邮件时遇到问题。代码为:
$msg = "some persian words";
$from = "my_email@my_server.com";
$headers = "From: $from'n";
$headers .= "MIME-Version: 1.0'n";
$headers .= "Content-type: text/html; charset=iso-8859-1'n";
$headers .= "BCC: $to".PHP_EOL;
$message = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Message</title></head>
<body style="margin:0px; font-family:Tahoma, Geneva, sans-serif; font-size:12px;">
'.htmlspecialchars_decode($msg).'
<div style="margin-top:5px">Please do not reply to this email.</div>
</div></body></html>';
我没有问题将文本内容发送到Gmail,但是在雅虎邮件中,波斯语字符的编码不起作用,如下所示:
تناسÙ"Ø ̈ر ù†Ù...ØªØ§Ø±Ø ̈Ù...نت
你应该使用Mail Mime
这是 git 中邮件 Mime 存储库的示例 https://github.com/pear/Mail_Mime/blob/master/scripts/phail.php
<?php
/**
* PHAIL - stands for PHP Mail
* @author Tomas V.V.Cox <cox@idecnet.com>
*/
require_once 'Mail.php';
require_once 'Mail/mime.php';
require_once 'Console/Getopt.php';
$argv = Console_Getopt::readPHPArgv();
$opts = Console_Getopt::getOpt($argv, 'f:c:s:t:a:b:');
if (PEAR::isError($opts)) {
usage($opts->getMessage());
}
PEAR::setErrorHandling(PEAR_ERROR_DIE);
$mime = new Mail_mime;
foreach ($opts[0] as $opt) {
$param = $opt[2];
switch ($opt[0]) {
case 'f':
$headers['From'] = $param; break;
case 'c':
$headers['Cc'] = $param; break;
case 's':
$headers['Subject'] = $param; break;
case 't':
$to = $param; break;
case 'a':
$mime->addAttachment($param); break;
case 'b':
$isfile = @is_file($param) ? true : false;
$mime->setTXTBody($param, $isfile); break;
}
}
$mbody = $mime->get();
$headers = $mime->headers($headers);
$mail = Mail::factory('mail');
$mail->send($to, $headers, $mbody);
function usage($error)
{
die($error);
}
?>