mail () in PHP with UTF-8 and umlauts

Sending emails with PHP is best implemented with powerful classes such as PHPMailer . Convenient wrapper functions make it possible to use UTF-8 content, embed images and send encrypted attachments with just a few lines of code. If you want to save yourself the overhead and, contrary to the recommendation, use the PHP function mail () , you will run into problems when using umlauts and UTF-8 at the latest.


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 GMX does not forgive the missing headers and shows a bad 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 display:

mail3

Back