PHP iconv_mime_encode() Function

Definition
In PHP, iconv_mime_encode() builds a MIME header field, such as an email subject, with a value that is safe to send.
The value is written as =?charset?scheme?text?= so that non-ASCII letters survive in a mail header.
Syntax
Call iconv_mime_encode() with the values you need. Start with this line.
$header = iconv_mime_encode("Subject", $text, $options);
| Call | What it does |
|---|
iconv_mime_encode("Subject", "Hello") | Returns "Subject: =?UTF-8?B?SGVsbG8=?=". |
iconv_mime_encode("Subject", $s, ["scheme" => "Q"]) | Uses quoted-printable instead of base64. |
The options array can hold scheme, input-charset, output-charset, line-length and line-break-chars.
Library declarations
iconv_mime_encode(string $field_name, string $field_value, array $options = []): string|false
Parameters
| Parameter | Description |
|---|
| field_name | Required. The header name, such as Subject. |
| field_value | Required. The text to encode. |
| options | Optional. An array with scheme (B or Q), input-charset, output-charset, line-length and line-break-chars. |
Return Value
| Return Value | Description |
|---|
| string|false | The header field with the encoded value, or false on failure. |
PHP Examples
Example 1 — Encode with the Q Scheme
Encode a subject with quoted-printable.
<?php
$options = [
"scheme" => "Q",
"input-charset" => "UTF-8",
"output-charset" => "UTF-8",
];
echo iconv_mime_encode("Subject", "Prüfung Prüfung", $options), PHP_EOL;
Subject: =?UTF-8?Q?Pr=C3=BCfung=20Pr=C3=BCfung?=
How It Works
- Set the scheme to
Q. It writes bytes as =XX. - Set both charsets. The text is UTF-8 in and out.
- Call iconv_mime_encode(). Pass the field name and the value.
- Read the result. A space is written as
=20.
Example 2 — Encode with the B Scheme
The B scheme uses base64 and is shorter for long text.
<?php
$options = [
"scheme" => "B",
"input-charset" => "UTF-8",
"output-charset" => "UTF-8",
];
echo iconv_mime_encode("Subject", "Prüfung Prüfung", $options), PHP_EOL;
Subject: =?UTF-8?B?UHLDvGZ1bmcgUHLDvGZ1bmc=?=
How It Works
- Set the scheme to
B. The text is turned into base64. - Keep the same charsets. Only the scheme changes.
- Compare the two results. Both decode to the same text.
- Choose Q for mostly ASCII text and B for text with many special letters.
Frequently Asked Questions
iconv_mime_encode() creates a MIME header field whose value is encoded so it can be used safely in an email header.
iconv_mime_encode(string $field_name, string $field_value, array $options = []): string|false.
B writes the text as base64. Q writes it as quoted-printable. Both are valid in a header.
It tells PHP what encoding your text is in. If it is wrong, the letters in the result will be wrong.
Use iconv_mime_decode() for a single field or iconv_mime_decode_headers() for many.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
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