I have a problem with sending email that contains a subject with accent. I've searched everywhere and all the solution doesn't work.
I have a string, the subject, probably encoded in iso-8859-1, sent via a 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 header of the mail :
$headers = "MIME-Version: 1.0" . $endl;
$headers .= 'Content-type: text/html; charset="iso-8859-1"' . $endl;
$headers .= 'Content-Transfer-Encoding: 8bit';
In result, I receive an email with ???? in subject
Note: tested in Windows, localhost
Sample code
testmail.php
<?php
$sender = "[email protected]";
$receiver = "[email protected]";
$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);
}
?>
Execute: php testmail.php Tested on Linux.
SOLUTION The correct solution to this problem is to use base64_encode
'=?UTF-8?B?' . base64_encode($subject) . '?=';
Ok, after many tests with the updated code on your question, here is the working solution based on your current code:
MAIL.PHP (file saved with UTF8 encoding)
<?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 (file saved with UTF8 encoding)
<?php
$sender = "[email protected]";
$receiver = "[email protected]";
$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)
));
?>
I have tested this and received all mails correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With