mail () in PHP with UTF-8 and umlauts

Emailing with PHP is best done with powerful classes like PHPMailer . Convenient wrapper functions make it possible to use content in UTF-8 with a few lines of code, to embed images and to send attachments encrypted. If you want to save the overhead and, contrary to the recommendation, use the PHP function mail () , you will encounter problems at the latest when using umlauts and UTF-8.


After calling the function with

mail(
   "vieldav@gmx.de",                          // Empfänger
   "ä ö ü ß",                                 // Betreff
   "ä ö ü ß",                                 // Inhalt
   "From: ä ö ü ß <david@vielhuber.de>"       // Header (Absender)
);

in a UTF-8 encoded file already leads to problems in the inbox: Outlook 2013 is still gracious and displays everything correctly:

mail1

But already GMX does not forgive the missing headers and shows an unsuccessful subject, sender and content:

mail2

If you send the correct headers (for example with base64_encode), you get

mail(
   "vieldav@gmx.de",                          				  // Empfänger
   "=?UTF-8?B?".base64_encode("ä ö ü ß")."?=",                            // Betreff
   "ä ö ü ß",                                 			 	  // Inhalt
   "Content-type: text/plain; charset=utf-8\r\n"
  ."From: =?UTF-8?B?".base64_encode("ä ö ü ß")."?=<david@vielhuber.de>"   // Header (Absender)
);

and finally GMX also shows a correct representation:

mail3

Back