The Quoted-Printable Trap in PHP Mail Headers
You are seeing collapsed newlines because your headers explicitly tell the mail server to use quoted-printable encoding (Content-Transfer-Encoding: quoted-printable). While this is a valid and often necessary encoding for text that contains non-ASCII characters or special symbols, it introduces a specific behavior regarding line breaks that you might not anticipate in PHP's mail() function.
When you set the header to quoted-printable, the mail transfer agent (MTA) takes your raw message body and encodes it before sending it across the network. The encoding algorithm converts characters outside the printable ASCII range into hexadecimal representations (e.g., = followed by two hex digits). Crucially, it also handles line folding.
Standard email lines are typically limited to 76 or 80 characters to avoid wrapping issues in older clients. When the encoded line exceeds this limit, the encoder inserts a soft break: an equals sign at the end of the line (=), with no actual newline character immediately following it in the raw data stream until the next logical block.
However, the issue you are facing stems from how PHP constructs the message and interacts with that encoding logic. In your example, the $message string contains standard CRLF sequences (\r\n). When the MTA processes a quoted-printable encoded body, it often performs "hard wrapping" or aggressive folding if it perceives the input as needing normalization.
The result is that your specific line break characters inside the PHP string are either being treated as part of the data to be folded or are being stripped because they don't align with the 76-character boundary in the encoded stream. This is why you see everything merged into a single line: CCCC CCCC CCCC CCC CCC CCC CCCC.
The fact that it works fine without the Content-Type header (assuming the default text/plain) confirms that the encoding switch is the culprit. Plain text does not require quoted-printable encoding, so the MTA passes your \r\n sequences directly to the recipient client, rendering them correctly as new lines.
The Correct Approach
To fix this while retaining the Content-Type declaration and ensuring newlines render correctly, you need to ensure that the line breaks in your PHP string are preserved exactly as intended before encoding takes place.
In many environments, simply using \r\n is sufficient, but the MTA might be folding lines based on character count rather than preserving explicit breaks if the content looks like it needs normalization. A robust solution involves ensuring your message body does not exceed the soft-wrap limit (usually 76 characters) for quoted-printable blocks, or explicitly managing the line endings.
Here is a refined approach to constructing the headers and message:
$to = 'AAAA <postmaster@xxx.xx>';
$subject = 'BBBB';
// Ensure the message uses standard CRLF line endings
$message = "CCCC\r\n" .
"CCCC CCCC CCC \r\n" .
"CCC \r\n" .
"CCC \r\n" .
"CCCC";
$headers = [
'From' => 'DDD <postmaster@xxx.xx>',
// Use text/html to allow for rich formatting if needed,
// but ensure the body is clean.
'Content-Type' => 'text/html; charset="UTF-8"',
'MIME-Version' => '1.0',
];
// Do not set Content-Transfer-Encoding manually in PHP headers.
// Let the mail() function or your server's MTA handle it automatically.
// Manually setting "quoted-printable" often forces aggressive folding
// that conflicts with explicit line breaks if not managed carefully.
mail($to, $subject, $message, implode("\r\n", $headers));
Why Removing the Manual Encoding Header Helps
In your original code, you manually set Content-Transfer-Encoding: quoted-printable. This is a directive to the MTA. However, modern MTAs are often smart enough to infer the correct encoding based on the Content-Type and the content itself. If you specify quoted-printable for a simple ASCII string containing only newlines, some servers may prioritize their own line folding logic over your explicit \r\n characters to meet RFC standards regarding maximum line length.
By removing the manual Content-Transfer-Encoding header (or letting PHP's mail() function determine it), you allow the MTA to choose the most appropriate encoding strategy. If the content is pure ASCII and simple, it might default to 7-bit encoding or handle quoted-printable folding more gracefully, preserving your explicit line breaks.
Trade-offs and Considerations
You mentioned wanting to use a Content-Type declaration. This is essential if you intend to send HTML emails or ensure character set consistency (UTF-8). The text/html content type does not inherently force quoted-printable encoding; it simply defines the format of the data.
If you require specific header control and must stick with quoted-printable, remember that this encoding expects lines to be broken every 76 characters. If your string contains a sequence like CCCC\r\n, the encoded output will look like CCCC=0D=0A. If the MTA sees a line longer than 76 characters in the encoded form, it will insert a soft break (=). If your logic relies on hard breaks (actual newlines) to separate distinct logical lines in the email body, you must ensure that no single encoded block exceeds the limit.
If your application is running on Laravel or another modern PHP framework, consider using the Mail facade instead of the raw mail() function. The framework's mailer can automatically set the correct headers and often handles encoding logic more robustly:
use Illuminate\Support\Facades\Mail;
Mail::raw('CCCC\r\nCCCC CCCC CCC \r\n', function ($message) {
$message->to('postmaster@xxx.xx')->subject('BBBB');
});
This approach abstracts away the manual header manipulation, reducing the risk of conflicting directives that cause line breaks to collapse. The key takeaway is that when mixing explicit line breaks with quoted-printable encoding, you must respect the 76-character limit in the encoded stream to prevent unwanted soft folds from overriding your hard breaks.
