Email from PHP has broken Subject header encoding

Email from PHP has broken Subject header encoding

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Email from PHP has broken Subject header encoding

When an email arrives in a mailbox with garbled characters appended to the subject line—like a^£ instead of clean text—it is almost always a mismatch between the declared character set and the actual byte stream sent over the wire. The issue you described, where the message body renders correctly but the header decodes into garbage, points directly to how SMTP servers interpret raw bytes versus how they expect encoded data.

Your current implementation sets Content-Type: text/plain; charset=UTF-8, which is correct for defining the character set of the payload. You also include Content-Transfer-Encoding: 8bit. This header tells the receiving mail server that the data stream contains plain bytes and should not be decoded or re-encoded by the SMTP protocol layer. While this is often safe, it relies heavily on the sending and receiving servers both agreeing on UTF-8 without ambiguity.

The problem arises because older mail transfer agents (MTAs) or specific mail clients sometimes struggle with raw UTF-8 in headers when they expect standard ASCII or ISO-8859-1 by default. When a server receives a header it does not recognize as valid ASCII, it may attempt to decode the bytes using its own internal logic, often defaulting to Latin-1 (ISO-8859-1). If your subject contains high-order UTF-8 bytes intended for characters like £ or €, but the receiver interprets those same byte sequences as Latin-1, they appear as control characters or strange glyphs. For instance, the UTF-8 two-byte sequence for £ might be misread as a control character followed by a printable symbol depending on the receiving stack's decoding rules.

To resolve this, you should switch to quoted-printable or base64 encoding for headers that contain non-ASCII characters. The Content-Transfer-Encoding: 8bit header is technically valid under RFC 2045, but many legacy systems ignore it and assume 7-bit ASCII. By explicitly changing the transfer encoding to quoted-printable, you signal to the receiving server that the data contains extended characters that need safe transport.

Here is how you can adjust your headers:

$headers = "From: $sender_name <$from>\n";
$headers .= "Reply-To: $sender_name <$from>\n";
$headers .= "X-Sender: $sender_name <$from>\n";
$headers .= "X-Mailer: PHP/8.0\n"; // Updated to reflect modern PHP versions
$headers .= "X-Priority: 3\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Date: $date\n";
$headers .= "Delivered-to: $to\n";
$headers .= "Return-Path: $sender_name <$from>\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\n"; // Changed from 8bit
$headers .= "Content-Type: text/plain; charset=UTF-8\n";

By setting Content-Transfer-Encoding: quoted-printable, you ensure that characters outside the ASCII range are encoded (e.g., £ becomes =C2=A3). This guarantees that even if a legacy server misinterprets the encoding, it will receive a stream of ASCII-safe bytes and can decode them correctly back into UTF-8.

Another critical consideration is ensuring that the actual header values themselves do not contain raw non-ASCII characters before you apply any encoding logic. If $sender_name or the subject string contains special characters, they must be encoded prior to insertion into the header array. You can use PHP’s built-in functions like mb_encode_mimeheader() if you are dealing with complex headers, though for simple text/plain bodies, switching the transfer encoding is usually sufficient to fix the ^£ corruption.

Always remember that the SMTP protocol was originally designed for 7-bit ASCII. While modern servers handle UTF-8 gracefully, the presence of Content-Transfer-Encoding: quoted-printable acts as a safety net against legacy infrastructure that might otherwise corrupt your headers. This approach ensures compatibility across diverse environments without sacrificing readability on modern clients.

Our network