PHP – Encode string for MIME header using mb_encode_mimeheader()

In PHP, mb_encode_mimeheader() function is used to encode a string for MIME (Multipurpose Internet Mail Extensions) header. It encodes a given string by the MIME header encoding scheme.


string mb_encode_mimeheader(str $string, str $charset, str $transfer_encoding, str $newLine, int $indent)


The mb_encode_mimeheader() function accepts five parameters −

  • $string − This parameter is used to encode the string. Its encoding should be the same as mb_internal_encoding()

  • $charset − This parameter specifies the character set name in which the string is represented.

  • $transfer_encoding − This parameter specifies the scheme of MIME encoding. It should be base64 (B) or Quoted-printable (Q). If not given, then it falls back to base64 (B).

  • $newLine − This parameter specifies the end-of-line (EOL) marker with which mb_encode_mimeheader() performs line-folding.

  • $indent − It is used for the indentation of the first line.

Return Values

It returns a converted version of the string that is represented in ASCII.

Example 1

   $name = "Online tutorials";
   $mbox = "nru";
   $doma = "gtin.thu";
   $addr = mb_encode_mimeheader($name,"UTF-7","Q")." <".$mbox."@".$doma.">";
   echo $addr;


Online tutorials

Example 2

   $string = "\xe2\x86\x92";
   mb_internal_encoding( "UTF-8");
   echo mb_encode_mimeheader($string, 'UTF-8');