带重音的PHP邮件

时间:2012-05-14 10:58:26

标签: php email diacritics

发送包含带重音主题的电子邮件时遇到问题。我到处搜索,所有的解决方案都不起作用。

我有一个字符串,主题,可能用 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) . '?=';

2 个答案:

答案 0 :(得分:1)

好的,经过对您问题的更新代码的多次测试后,这是基于您当前代码的工作解决方案:

MAIL.PHP (使用UTF8编码保存的文件)

<?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' . "\r\n";
$headers.= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers.= 'Content-Transfer-Encoding: 8bit';

// Message
$msg = "
<html>
  <head>
    <title></title>
  </head>
  <body>
    $msg
  </body>
</html>";

$receiver = explode(',', $receiver);

foreach ($receiver as $rec) {
    mail($rec, $subject, $msg, $headers);
    sleep(5);
}

?>

TESTMAIL.PHP (使用UTF8编码保存的文件)

<?php

$sender = "inbox@domain.com";
$receiver = "outbox@domain.com";
$subject = "Accentued éàèçô letters";
$msg = "Accentued éàèçô letters";

setlocale(LC_CTYPE, "en_US.UTF-8");
shell_exec(sprintf('"LANG=en_US.UTF-8"; php mail.php %s %s %s %s > /dev/null 2>/dev/null &',
                        escapeshellarg($sender),
                        escapeshellarg($receiver),
                        escapeshellarg($subject),
                        escapeshellarg($msg)
                    ));
?>

我已对此进行了测试并正确接收了所有邮件。

答案 1 :(得分:0)

如果电子邮件的标头不是7位US-ASCII,则需要对其进行RFC2047编码。您已经拥有的MIME声明覆盖了正文,而不是标题。

通常情况下,除非您了解电子邮件的工作原理,否则最方便的解决方案是找到一个能够为您正确处理这些内容的库。我不熟悉PHP,但这是StackOverflow上的一个常见问题,答案通常似乎涉及PEAR。