Handling Line Breaks in text/plain Email Bodies
When sending emails via SMTP using a library like PHPMailer or similar wrappers, it is easy to assume that setting the MIME type to text/plain is sufficient for preserving formatting. You might write code that looks exactly like this:
$subject = 'Password Reset Request';
$body = "Did you request a password reset for your account?\r\n\r\nIf yes, click here:\nhttp://www.website.com";
$mail->isHTML(false);
$mail->Subject = $subject;
$mail->Body = $body;
Despite setting the header to Content-Type: text/plain; charset=utf-8, the recipient often sees a single block of text where line breaks have been collapsed, or worse, the raw \r\n characters appear as literal text. This happens because email clients do not interpret whitespace in plain text bodies exactly like a web browser interprets HTML <br> tags or CSS white-space.
The core issue lies in how modern email clients handle "soft wraps" versus explicit line breaks. When a client receives a message with text/plain, it attempts to render the content based on its own width constraints. If your source string contains only spaces, tabs, or non-breaking spaces ( ), the client will not display them as visible gaps; it will simply ignore them and reflow the text to fit the screen.
To force a line break that every email client respects, you must use the carriage return and newline combination: \r\n. This is the standard CRLF sequence defined in RFC 5322. While many developers instinctively use \n (Unix-style) or just spaces hoping for a visual gap, these are often ignored by mail clients treating the body as a continuous stream of text.
The Correct Sequence: CRLF vs LF
In the provided example from the source material, the code uses \r\n\r\n between paragraphs, which is technically correct for separating major blocks of text. However, if you look at the line immediately preceding the link:
"If yes, click here:\nhttp://www.website.com"
The use of a single \n might be interpreted differently depending on the library's internal message folding logic. While \r\n is the strict SMTP standard, some libraries normalize line endings automatically. The real danger appears when you rely on spaces or tabs to create indentation or gaps in plain text. Email clients will strip these out entirely if they do not correspond to a hard break.
If your goal is to ensure that "If yes," and "click here:" appear on separate lines, the string must explicitly contain a \r\n sequence at that boundary. There is no universal HTML-like tag you can insert in a text/plain body to achieve this; the raw control characters are the only reliable method.
Handling Long Lines and Word Wrapping
Even with the correct \r\n sequences, there is a secondary consideration: word wrapping. If your plain text email contains very long linesâsuch as a URL or a long paragraph without spacesâthe recipient's email client may decide to wrap the text at an arbitrary point. This can lead to poor readability if the client wraps in the middle of a sentence or breaks a URL.
To mitigate this, you should limit line lengths to around 76 characters. This is the traditional SMTP standard for message body folding. If you are generating dynamic content, consider splitting long strings programmatically before assigning them to the email body variable.
$maxLineLength = 76;
$lines = preg_split('/[\r\n]+/', $body); // Ensure existing breaks are kept
$newBody = '';
foreach ($lines as $line) {
if (strlen($line) > $maxLineLength) {
$chunks = str_split($line, $maxLineLength);
$newBody .= implode("\r\n", $chunks) . "\r\n";
} else {
$newBody .= $line . "\r\n";
}
}
This approach ensures that no single line exceeds the recommended limit, reducing the likelihood of email clients performing unwanted soft-wrapping.
When to Use text/html
If your use case involves complex formatting, such as tables, bold text, or images, plain text is not the right tool. In those scenarios, you should switch the content type to text/html. This allows you to use standard HTML tags like <br>, <p>, and <div> to control layout precisely.
However, if you strictly require a simple, unstyled message that renders consistently across legacy systems and mobile clients without rendering engines, sticking to text/plain with explicit \r\n line breaks is the most robust strategy. Just remember that spaces and tabs are invisible in this context; only CRLF sequences create the visual separation your users expect.