I have a problem sending emails containing a subject with an accent. I searched everywhere and all solutions do not work.
I have a line, the theme is probably encoded in iso-8859-1 sent via the command line script
$res = shell_exec(sprintf("php mail.php %s %s %s %s",
escapeshellarg($email->sender),
escapeshellarg($receiver->email),
escapeshellarg($email->subject),
escapeshellarg($email->message)
));
And the message header:
$headers = "MIME-Version: 1.0" . $endl;
$headers .= 'Content-type: text/html; charset="iso-8859-1"' . $endl;
$headers .= 'Content-Transfer-Encoding: 8bit';
As a result, I get an email with ???? in topic
Note: checked on windows localhost
Code example
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";
$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);
}
?>
Run: php testmail.php Tested on Linux.
SOLUTION The
correct solution to this problem is to use base64_encode
'=?UTF-8?B?' . base64_encode($subject) . '?=';