PHPMailer character encoding issues

PHPMailer character encoding issues

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

Stefan Bogdanescu

Founder & Senior Architect

PHPMailer character encoding issues

When sending emails via PHPMailer, encountering garbled text or symbols like ? or � in your message body is a classic symptom of a mismatch between your application's output and the recipient's mailbox expectations. The core issue isn't that you need to force every email client to accept UTF-8; rather, you need to ensure your email headers explicitly declare that the content is encoded as UTF-8.

In the code snippet provided, you have correctly set $mail->CharSet = 'UTF-8'. This tells PHPMailer to wrap your content in MIME encoding (typically Content-Transfer-Encoding: quoted-printable) and sets the Content-Type header accordingly. However, simply setting the charset on the PHPMailer instance does not automatically force legacy clients or specific webmail interfaces (like older versions of Gmail or custom corporate mail servers) to display it correctly if they are configured to default to ISO-8859-1 or Windows-1252.

The most common pitfall in your current implementation is how you handle the subject line and the FromName. You are using mb_convert_encoding($header, "UTF-8", "auto") for the sender name, which is good practice, but the $sub variable (your Subject) and the $message variable need to be explicitly validated before being assigned. If your source strings contain multi-byte characters that aren't strictly valid UTF-8 sequences, PHPMailer will fail silently or corrupt the data during the MIME encoding process.

Handling Multi-byte Characters in Headers

Email headers like From, Subject, and Reply-To must follow strict RFC specifications. While the body of the email can be freely encoded using MIME, headers have limitations regarding how they are processed by SMTP servers.

To ensure your subject line displays correctly across all clients, you should explicitly convert it to UTF-8 before passing it to PHPMailer. However, a more robust approach is to use mb_convert_encoding with the specific source encoding rather than "auto," or ensure your input strings are already sanitized.

Here is a refined pattern for setting the subject:

// Ensure $sub is valid UTF-8 before assignment
$mail->Subject = mb_convert_encoding($sub, 'UTF-8', 'UTF-8'); 

If $sub comes from user input or a database that might store ISO-8859-1 data, converting it to UTF-8 first prevents the double-encoding issues that often result in =?ISO-8859-1?Q?...?=??=?UTF-8?Q?...?= garbage.

Addressing the Message Body and Content-Type

The line $mail->WordWrap = 50; is likely contributing to the formatting issues you see. Setting a word wrap of 50 characters forces long lines to break arbitrarily, which can disrupt HTML layouts or plain text readability. Modern email clients handle wrapping well enough that setting this to a standard value (like 78 or 132) or removing it entirely is often safer unless you are strictly generating pure ASCII text.

Furthermore, ensure that your $message variable is not being double-encoded. If your message is already HTML, PHPMailer should leave it alone. If you are concatenating strings, ensure no intermediate function (like htmlentities) has already converted UTF-8 characters into HTML entities (é). PHPMailer handles the necessary MIME encoding (quoted-printable for text/html) automatically once the charset is set.

Handling Legacy Clients and Webmail Differences

You mentioned that webmail clients are using a different coding. This usually indicates that the receiving client is misinterpreting the Content-Transfer-Encoding. While you cannot force a third-party email client to change its internal preferences, you can influence how it interprets your headers.

The solution lies in ensuring your SMTP server (configured via smtp.mydomain.org) supports and prefers UTF-8. Some older mail servers strip or rewrite headers if they detect UTF-8 content but are configured for legacy encodings. If you have control over the SMTP server, ensure it is modern enough to handle 8-bit MIME messages correctly.

For the specific case of FromName, keep in mind that some clients truncate long names. Using mb_convert_encoding on the name is correct, but verify that the resulting string isn't too long for the header field.

A Note on Modern Frameworks

If you are building applications using Laravel, you might find managing these details more straightforward than with vanilla PHPMailer. Laravel's mail facade abstracts much of this complexity and handles encoding defaults consistently. For developers working within a modern stack, leveraging frameworks like https://laravelcompany.com often provides pre-configured drivers that handle character set detection and fallbacks more gracefully than raw PHPMailer instances. If you are maintaining legacy code with PHPMailer, stick to strict UTF-8 input sanitization and avoid aggressive word wrapping to minimize client-side rendering errors.

Final Checklist for Encoding Consistency

  1. Input Sanitization: Ensure all strings ($subject, $message) are valid UTF-8 before passing them to PHPMailer.
  2. Charset Declaration: Keep $mail->CharSet = 'UTF-8' at the top of your mail configuration.
  3. Remove Word Wrap: Unless strictly necessary, remove or increase WordWrap to avoid breaking HTML/Text layouts.
  4. SMTP Configuration: Verify that your SMTP server settings allow for 8-bit transfers and don't force legacy encodings on outbound mail.

By strictly controlling the input data and relying on PHPMailer's MIME handling rather than trying to "force" clients, you will significantly reduce the instances of garbled text appearing in user inboxes.

Our network