我在发送包含带有口音的主题的电子邮件时遇到问题。我到处搜索,所有解决方案都不起作用。
我有一个字符串,主题,可能编码为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 中测试
示例代码
测试邮件.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)
));
?>
邮件.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 上测试。
SOLUTION 这个问题的正确解决方法是使用base64_encode
'=?UTF-8?B?' . base64_encode($subject) . '?=';