PHP iconv_mime_decode_headers() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iconv

Definition

In PHP, iconv_mime_decode_headers() decodes several MIME header lines at once and returns them as an array.

Each header name is a key. A name that appears more than once becomes an array of values.

Syntax

Call iconv_mime_decode_headers() with the values you need. Start with this line.

PHP
$headers = iconv_mime_decode_headers($raw, 0, "UTF-8");
CallWhat it does
iconv_mime_decode_headers($raw, 0, "UTF-8")Returns the decoded headers as an array.
iconv_mime_decode_headers($raw)Uses the default output encoding.

Separate the header lines with \r\n, as in a real email.

Library declarations
PHP
iconv_mime_decode_headers(string $headers, int $mode = 0, ?string $encoding = null): array|false

Parameters

ParameterDescription
headersRequired. The header lines, one after the other.
modeOptional. A bit mask of ICONV_MIME_DECODE_* constants. Default is 0.
encodingOptional. The character set of the result.

Return Value

Return ValueDescription
array|falseAn array of headers, or false on failure.

PHP Examples

Example 1 — Decode Two Headers

Turn a short header block into an array.

PHP
<?php
$raw = "Subject: =?UTF-8?Q?Pr=C3=BCfung?=\r\nTo: [email protected]";

print_r(iconv_mime_decode_headers($raw, 0, "UTF-8"));

How It Works

  1. Join the lines with \r\n. This is how headers are separated.
  2. Call iconv_mime_decode_headers(). Pass UTF-8 as the output.
  3. Get one key per header. The subject is decoded.
  4. Read the values. Plain headers are kept as they are.

Example 2 — Handle a Repeated Header

When a name appears twice, the value becomes an array.

PHP
<?php
$raw = "X-Tag: one\r\nX-Tag: two";

print_r(iconv_mime_decode_headers($raw, 0, "UTF-8"));

How It Works

  1. Repeat a header name. Here X-Tag appears twice.
  2. Decode the block. Both values are kept.
  3. Read a nested array. Index 0 is the first value.
  4. Test with is_array(). It tells you which form you have.

Example 3 — Read the From Header

Decode a sender name and print it.

PHP
<?php
$raw = "From: =?UTF-8?Q?Andr=C3=A9?= <[email protected]>";

$headers = iconv_mime_decode_headers($raw, 0, "UTF-8");

echo $headers["From"], PHP_EOL;

How It Works

  1. Use an encoded name with an address. The address stays plain.
  2. Decode the headers. The name becomes André.
  3. Read the key From. Header names keep their case.
  4. Print the value. You see the name and the address.

Frequently Asked Questions

iconv_mime_decode_headers() decodes a block of MIME header lines and returns an array with one entry per header.
iconv_mime_decode_headers(string $headers, int $mode = 0, ?string $encoding = null): array|false.
Its value becomes an array that holds each occurrence in order.
iconv_mime_decode() decodes one field and returns a string. iconv_mime_decode_headers() decodes a whole block and returns an array.
It is optional, but passing UTF-8 makes the result the same on every server.

Next: iconv_mime_encode()

iconv_mime_encode() builds a MIME header field with an encoded value.

iconv_mime_encode() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful