Accent php mail

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";

//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);
}
?>

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) . '?=';
+3
source share
2 answers

Well, after many tests with updated code to your question, here is a working solution based on your current code:

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

RFC2047 , 7- US-ASCII. MIME, , , .

, , - , . PHP, StackOverflow, , , PEAR.

0

All Articles