PHP – Compose a MIME header field using iconv_mime_encode() function

PHPServer Side ProgrammingProgramming

In PHP, iconv_mime_encode() function is used to composes a MIME header field. This is an inbuilt PHP function.

Syntax

string iconv_mime_encode(string $field_name, string $field_value, array $options=[])

The iconv_mime_encode() function is used to compose and return a string that represents a valid MIME header field, which looks like -

Subject: =ISO-8859-1?Q?Pr=FCfung_f=FFCr?= Entwerfen von einer MIME kopfzeile

Note− In the above example, Subject - is the field name, and the portion that begins with "=ISO-8859-1?..." is the field value.

Parameters

iconv_mime_encode() accepts three different parameters $field_name, $field_value and $options.

  • $field_name − This parameter is used for the field name.

  • $field_value − This parameter is used for the field value.

  • $options − Using this parameter, you can control the behavior of the iconv_mime_encode() by specifying an associative array that contains configuration items to the optional parameter.

Following is the list of configuration items supported by iconv_mime_encode()

Item

Type

Description

Default Value

Example

scheme

string

The scheme specifies the method to encode a field value by. This item value may be either B (base64) or Q(quoted-printable) encoding scheme. 


input-charset

string

It specifies the character set, the field_name is the first parameter and field_value is the second parameter. If these parameters are not given then the iconv_mime_encode() function assumes that it may be presented in the iconv.internal_charset ini setting.

iconv.internal_charset

ISO-8859-1

output-charset

string

It specifies the character set to use to compose the MIME header. If it is not given then it will use the input-charset value.

input_charset is use as a default value

UTF-8

line-length

integer

It specifies the maximum length of the header lines.

76

996

line-break-chars

string

It specifies the sequence of characters to append to each line as an EOL when folding is performed on a long header field. If it is not given then this default to “\r\n” (CR LF)

\r\n

\n

Example 1 - Using "Q" quoted-printable encoding scheme

 Live Demo

<?php
   // used configuration items supported by iconv_mime_encode()
   $options = array(
      "input-charset" => "ISO-8859-2",
      "output-charset" => "UTF-8",
      "line-length" => 76,
      "line-break-chars" => "\n"
   );
   // Q quoted-printable encoding scheme is used
   $options["scheme"] = "Q";

   // Below code will show the result as
   // "Subject: =?UTF-8?Q?Pr=C3=BCfung=20Pr=C3=BCfung?="
   echo iconv_mime_encode("Subject", "Prüfung Prüfung", $options);
?>

Output

Subject: =?UTF-8?Q?Pr=C3=83=C2=BCfung=20Pr=C3=83=C2=BCfung?=

Example 2

 Live Demo

<?php
   // used configuration items supported by iconv_mime_encode()
   $options = array(
      "input-charset" => "ISO-8859-1",
      "output-charset" => "UTF-8",
      "line-length" => 76,
      "line-break-chars" => "\n"
   );

   // B base64 encoding scheme is used
   $options["scheme"] = "B";

   // Below code will show the result as
   //"Subject: =?UTF-8?B?UHJlw4PCp29zIE9sw4PCoC50eHQ=?="
   echo iconv_mime_encode("Subject", "Preços Olà.txt", $options);
?>

Output

Subject: =?UTF-8?B?UHJlw4PCp29zIE9sw4PCoC50eHQ=?=
raja
Updated on 21-Aug-2021 15:10:01

Advertisements