2

I try to include a numerical variable from a database into a mail, using utf-8 encoding.

Here is the php code:

$req = $bdd->prepare('SELECT prix FROM offres WHERE rang=?');
$req->execute(array(1));
$donnees=$req->fetch();

$passage_ligne = "\n";

$v = 10.15;
$text='essai1 = '.$v;
$text.=$passage_ligne;
$text.= 'essai2 = '.$donnees['prix'];
$text.=' here is a french accent: é';
$v=$donnees['prix'];
$text.=$passage_ligne."essai3 = ".$v;

echo $text;

$from='xxx@wanadoo.fr';
$replyto='xxx@wanadoo.fr';
$Xmailer='PHP/';

$header = 'From: '. $from . $passage_ligne .'Reply-To: '. $replyto . $passage_ligne .'X-Mailer: '. $Xmailer . phpversion();
$header.= "MIME-Version: 1.0". $passage_ligne;
$header.= "Content-Type: text/plain; charset=utf-8". $passage_ligne;
$header.= "Content-Transfer-Encoding: quoted-printable". $passage_ligne;
$passage_ligne .'X-Mailer: '. $Xmailer . phpversion();

mail('xxxr@wanadoo.fr','sujet',$text,$header);

The numerical variable $donnees['prix'] = 18 does show up on the screen:

essai1 = 10.15 essai2 = 18 here is a french accent: é essai3 = 18

But not in the mail, where it is truncated to "8":

essai1 = 10.15
essai2 =8 here is a french accent: é
essai3 =8

If I use the following header:

$header = 'From: '. $from . $passage_ligne .'Reply-To: '. $replyto . $passage_ligne .'X-Mailer: '. $Xmailer . phpversion();

The numerical variable shows up corectly in the mail, but not the accents:

essai1 = 10.15
essai2 = 18 here is a french accent: é
essai3 = 18

Any way to get both in the same mail ? - Thanks

devlin carnate
  • 8,309
  • 7
  • 48
  • 82
Luc
  • 23
  • 2
  • Related? [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – FirstOne Dec 18 '17 at 18:18
  • I tried to set the database to utf8-mb4 with no change. I have just noticed: the popup of Mozilla Thunderbird annoucing the mail correctly displays the text. It is only in the mail body that the text is truncated (the mail source is also correct). – Luc Dec 18 '17 at 18:34

1 Answers1

1

The = character has special meaning in quoted-printable encoding. It's used as the prefix for escape sequences; it's followed by 2 hex digits to encode non-printing characters.

In your case, you're sending = 18. The two characters after = are taken to be the hex code 1.

If you want to send a literal = in quoted-printable mail, you need to encode it as =3D.

$v = 10.15;
$text='essai1 =3D '.$v;
$text.=$passage_ligne;
$text.= 'essai2 =3D '.$donnees['prix'];
$text.=' here is a french accent: é';
$v=$donnees['prix'];
$text.=$passage_ligne."essai3 =3D ".$v;

Or you could use the quoted_printable_encode function.

mail('xxxr@wanadoo.fr','sujet',quoted_printable_encode($text),$header);

The latter is probably the best solution, it might also solve your problem with the accented characters.

Barmar
  • 741,623
  • 53
  • 500
  • 612