PHP iconv_mime_encode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iconv

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.

PHP
$header = iconv_mime_encode("Subject", $text, $options);
CallWhat 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
PHP
iconv_mime_encode(string $field_name, string $field_value, array $options = []): string|false

Parameters

ParameterDescription
field_nameRequired. The header name, such as Subject.
field_valueRequired. The text to encode.
optionsOptional. An array with scheme (B or Q), input-charset, output-charset, line-length and line-break-chars.

Return Value

Return ValueDescription
string|falseThe 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
<?php
$options = [
    "scheme" => "Q",
    "input-charset" => "UTF-8",
    "output-charset" => "UTF-8",
];

echo iconv_mime_encode("Subject", "Prüfung Prüfung", $options), PHP_EOL;

How It Works

  1. Set the scheme to Q. It writes bytes as =XX.
  2. Set both charsets. The text is UTF-8 in and out.
  3. Call iconv_mime_encode(). Pass the field name and the value.
  4. 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
<?php
$options = [
    "scheme" => "B",
    "input-charset" => "UTF-8",
    "output-charset" => "UTF-8",
];

echo iconv_mime_encode("Subject", "Prüfung Prüfung", $options), PHP_EOL;

How It Works

  1. Set the scheme to B. The text is turned into base64.
  2. Keep the same charsets. Only the scheme changes.
  3. Compare the two results. Both decode to the same text.
  4. Choose Q for mostly ASCII text and B for text with many special letters.

Example 3 — Encode and Decode Again

Round trip: encode a header and read it back.

PHP
<?php
$header = iconv_mime_encode("Subject", "Hello", [
    "input-charset" => "UTF-8",
    "output-charset" => "UTF-8",
]);

echo $header, PHP_EOL;
echo iconv_mime_decode($header, 0, "UTF-8"), PHP_EOL;

How It Works

  1. Leave out the scheme. The default scheme is B.
  2. Print the encoded header. SGVsbG8= is base64 for Hello.
  3. Decode it with iconv_mime_decode().
  4. Get the original. You see Subject: Hello again.

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.

Next: iconv_set_encoding()

iconv_set_encoding() changes an iconv character set setting (deprecated).

iconv_set_encoding() 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