PHP Mail header for emails with special characters in the subject or message
When using mail() to send HTML emails containing Unicode characters, you will often encounter two distinct failure modes. First, spaces and newlines disappear entirely from the body of the email. Second, special characters in the subject line render as garbled entities like ø&... instead of their intended symbols. These issues stem from a fundamental misunderstanding of how SMTP handles character encoding versus how PHP's basic mail function constructs headers.
The root cause lies in your header configuration. You are explicitly setting the content type to ISO-8859-1, but you have not declared a proper MIME charset for the message body, nor have you set the correct boundary parameters if you were using multipart content. More critically, the mail() function in PHP does not automatically encode non-ASCII characters in headers when they are passed as raw strings. If your subject contains bytes outside the range of 0-127, standard SMTP servers will often try to interpret them based on the assumed charset (usually US-ASCII), resulting in the display of HTML entities or replacement characters.
To fix the spacing issue, you must ensure the message body is treated as a specific character set within the headers. However, simply adding Content-Type: text/html; charset=UTF-8 is not enough if the underlying encoding of your input strings ($_REQUEST['message']) does not match UTF-8. If the input is ISO-8859-1 but you declare UTF-8 in the header, the browser will misinterpret every character as a different symbol. Conversely, if the input is UTF-8 and you declare ISO-8859-1, special characters will appear incorrectly. The safest approach is to ensure your PHP variables are encoded in UTF-8 before passing them to mail(), and then explicitly declaring that encoding in the headers.
Regarding the subject line, the header format Subject: =?charset?encoding?encoded-string? is required for non-ASCII characters. However, the native mail() function does not support this complex encoding syntax directly. It expects a plain string. If you pass a UTF-8 string containing special characters to the Subject parameter of mail(), the underlying C library often fails to encode it properly for SMTP transmission, leading to the entity soup seen in your logs (ʼ).
The most robust solution is to stop relying on PHP's mail() function for headers requiring specific encoding and instead use a dedicated mailer that handles MIME boundaries and encoding automatically. In modern Laravel applications, this is handled by the framework's mailer stack, which defaults to UTF-8 and correctly encodes subjects and bodies. If you are building a standalone script or using a legacy setup, consider switching to PHPMailer or Symfony Mailer, as they provide the necessary logic to convert Unicode characters into RFC 2047 encoded words without manual intervention.
If you must stick with the native mail() function, you have to manually encode the subject string before passing it to the function, though this is error-prone and often leads to security vulnerabilities if not sanitized perfectly. A better alternative for simple scripts without a full framework is to use imap_send_message or switch to an SMTP client library that handles the RFC 5322 header encoding requirements correctly.
Ultimately, the problem isn't just about adding a charset; it's about ensuring the entire pipelineâfrom input retrieval to SMTP transmissionâspeaks a consistent language. Declaring Content-Type: text/html; charset=UTF-8 is necessary but insufficient if the underlying data isn't UTF-8 or if the subject header lacks proper encoding support. For production applications, relying on a mature mailer library that abstracts these RFC 5322 complexities is the only reliable path to ensuring special characters display correctly in both the subject and the body.