【发布时间】:2012-05-21 20:37:08
【问题描述】:
我在发送包含重音主题的电子邮件时遇到问题。我到处搜索,所有解决方案都不起作用。
我有一个字符串,主题,可能以 iso-8859-1 编码,通过命令行脚本发送
$res = shell_exec(sprintf("php mail.php %s %s %s %s",
escapeshellarg($email->sender),
escapeshellarg($receiver->email),
escapeshellarg($email->subject),
escapeshellarg($email->message)
));
以及邮件的标题:
$headers = "MIME-Version: 1.0" . $endl;
$headers .= 'Content-type: text/html; charset="iso-8859-1"' . $endl;
$headers .= 'Content-Transfer-Encoding: 8bit';
结果,我收到一封带有 ???? 的电子邮件在主题中
注意:在 Windows、localhost 中测试
示例代码
testmail.php
<?php
$sender = "me@gmail.com";
$receiver = "you@mgmail.com";
$subject = "Accentued éàèçô letters";
$msg = "Accentued éàèçô letters";
shell_exec(sprintf('LANG="fr_FR.iso88591"; php mail.php %s %s %s %s > /dev/null 2>/dev/null &',
escapeshellarg($sender),
escapeshellarg($receiver),
escapeshellarg($subject),
escapeshellarg($msg)
));
?>
mail.php
<?php
if ($argc!=5) exit;
$sender = $argv[1];
$receiver = $argv[2];
$subject = $argv[3];
$msg = $argv[4];
$endl = "\r\n";
//HTML header
$headers = "From: $sender" . $endl;
$headers .= "MIME-Version: 1.0" . $endl;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $endl;
$headers .= 'Content-Transfer-Encoding: 8bit';
$msg = "
<html>
<head>
<title></title>
</head>
<body>
$msg
</body>
</html>
";
$receiver = explode(',', $receiver);
foreach ($receiver as $rec)
{
mail($rec, $subject, $msg, $headers);
sleep(5);
}
?>
执行:php testmail.php 在 Linux 上测试。
解决方案 这个问题的正确解决方法是使用base64_encode
'=?UTF-8?B?' . base64_encode($subject) . '?=';
【问题讨论】:
-
stackoverflow.com/questions/1719149/… 见已接受答案的底部。
-
可能已编码? 正确的标点和重音取决于所使用的编码! 可能不好!您需要确定正在使用的编码是什么!
-
@peterbond:试过了。首先,我应该使用的不是内容类型 text/plain。其次,尝试了 base64 编码但没有运气。问题在于命令行编码,在 Windows 中,它是 Windows-1252。在其他系统中,可能会有所不同。
-
@Zuul:它来自一个 HTML 表单,它是 iso-8859-1,所以它必须是 iso-8859-1
-
您设置标头并发送使用 iso-8859-1 编码的电子邮件的 PHP 文件是吗?
标签: php email diacritics