PHP – Encode string for MIME header using mb_encode_mimeheader()

PHPServer Side ProgrammingProgramming

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.

Syntax

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

Parameters

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

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

Output

Online tutorials

Example 2

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

Output

=?UTF-8?B?4oaS?=
raja
Published on 11-Oct-2021 12:40:01
Advertisements