Current Location: Home> Latest Articles> How to Build MIME Header Fields with iconv_mime_encode() Function in PHP

How to Build MIME Header Fields with iconv_mime_encode() Function in PHP

gitbox 2025-06-12

Introduction

In network communication, MIME (Multipurpose Internet Mail Extensions) is a widely used message format specification commonly applied in email and HTTP. Every MIME message contains a MIME header describing the message and an optional message body. The MIME header includes metadata such as sender, recipient, subject, timestamp, and more. This article introduces how to use the PHP iconv_mime_encode() function to build a MIME header field.

iconv_mime_encode() Function

Function Purpose

The iconv_mime_encode() function is used to convert an associative array into a MIME header field.

Function Syntax

The basic syntax of the function is as follows:


string iconv_mime_encode(string $field_name, string $field_value, array $options = array());

Parameter Explanation

$field_name: The name of the MIME header field, such as "From", "To", "Subject", etc.

$field_value: The value of the MIME header field.

$options: An optional array of parameters, including:

  • "scheme": The MIME encoding scheme, which can be Q (Quoted-Printable) or B (Base64).
  • "input-charset": The input character set, defaulting to the system character set.
  • "output-charset": The output character set, defaulting to the system character set.
  • "line-length": The maximum number of characters per line, defaulting to 76.
  • "line-break-chars": The line separator, defaulting to "\r\n".
  • "scheme-folding": Whether to fold long lines in MIME encoding, defaulting to true.

Return Value

The function returns a string that has been MIME-encoded.

Example

The following example demonstrates how to use the iconv_mime_encode() function to build a MIME header for an email:


// Define the email subject and body
$subject = "Test Email";
$body = "This is a test email, please do not reply.";

// Define the sender and recipient information
$from_name = "Sender";
$from_address = "[email protected]";
$to_name = "Recipient";
$to_address = "[email protected]";

// Define the email headers
$headers = array(
    "From" => $from_name . " <" . $from_address . ">",
    "To" => $to_name . " <" . $to_address . ">",
    "Subject" => $subject,
    "Reply-To" => $from_address, // Reply address
    "MIME-Version" => "1.0", // MIME version
    "Content-Type" => "text/plain;charset=utf-8",
);

// Build the email headers
foreach ($headers as $key => $value) {
    $encoded_value = iconv_mime_encode($key, $value);
    // Add the encoded header to the array
    $headers_array[] = $key . ": " . $encoded_value;
}

// Join the headers array into a string with "\r\n" separator
$headers_string = implode("\r\n", $headers_array);

// Send the email
mail($to_address, $subject, $body, $headers_string);

Explanation of the Example

In the above example, we first define the subject and body of the email, then set the sender and recipient information. Next, we define an associative array containing the email header information. "MIME-Version" specifies the MIME version, and "Content-Type" specifies the email content type and character encoding. We then loop through the array, encoding each field using the iconv_mime_encode() function. Finally, we use the implode() function to join all header information with "\r\n" separators and pass it as the fourth parameter to the mail() function to send the email.

Important Notes

When using the iconv_mime_encode() function to generate MIME header information, make sure the input and output character sets match to avoid potential encoding issues.

Although the Content-Type for the email body has been set to "text/plain;charset=utf-8", you should still handle cases where the email body contains non-ASCII characters, and adjust character encoding accordingly based on the actual content.

Conclusion

By using the iconv_mime_encode() function, PHP developers can easily build MIME header fields, making it efficient for tasks like sending emails. This article covered the function’s usage, parameters, and return values, as well as providing example code to help developers apply the function in real-world projects.