Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mail with accent

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) . '?=';
like image 687
Thanh Trung Avatar asked Dec 05 '25 05:12

Thanh Trung


1 Answers

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.

like image 179
Zuul Avatar answered Dec 07 '25 20:12

Zuul