Newline Not Working in PHP mail
When a PHP script sends an email using the mail() function, line breaks often disappear or render as plain text depending on the recipient's client. This behavior is frequently misinterpreted by developers who expect \n or \r\n to act like physical carriage returns and line feeds within the raw message body. In your specific case, where a Thunderbird 3 user receives "Hi $fname.\r\n Your entries..." as a single continuous string, the root cause lies not in PHP's logic but in how email protocols handle whitespace and encoding.
The mail() function passes the $message variable directly to the server's mail transfer agent (MTA). While the SMTP protocol technically supports CRLF (\r\n) line endings for headers and message bodies, many client applications apply their own normalization rules. When an email client like Thunderbird receives a message where the body starts immediately after the Content-Transfer-Encoding header without proper separation or when the encoding is interpreted strictly as 7-bit ASCII, special characters like \r and \n are often treated as literal text rather than control codes if they appear in a context that suggests raw string concatenation without explicit encoding flags.
In the code snippet you provided:
$message = 'Hi '.$fname.', \r\n Your entries for the week of '
.$weekof.' have been reviewed. \r\n Please login and View Weekly reports to see the report and comments. \r\n Thanks, \r\n'.$myname;
mail($to, $subject, $message, $from);
PHP correctly outputs the characters \, r, \, and n as literal backslashes followed by letters if you did not intend them to be escape sequences. However, assuming your intention was indeed to insert actual line breaks, the issue arises because the mail() function relies on the underlying MTA to format the message. If the resulting email is transmitted with a Content-Transfer-Encoding: 7bit header, the recipient's client expects the text to be plain ASCII. Some older or stricter clients might misinterpret raw \r\n sequences in the body if they are not strictly formatted as CRLF at the byte level, although PHP strings usually handle this correctly unless the environment is misconfigured.
The most reliable fix involves ensuring that the message body uses actual line breaks in the string and forcing a specific transfer encoding. Modern best practices recommend using Content-Type: text/plain and explicitly setting the encoding to quoted-printable or base64 if special characters are present. This forces the MTA to handle the newline conversion correctly before transmission.
Consider updating your script to use quoted-printable encoding, which handles line breaks robustly across all clients:
$message = "Hi {$fname},\r\nYour entries for the week of {$weekof} have been reviewed.\r\nPlease login and View Weekly reports to see the report and comments.\r\nThanks,\r\n{$myname}";
$headers = "From: {$from}\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n";
mail($to, $subject, $message, $headers);
Notice the explicit Content-Type header. Without it, some servers default to text/html, which can cause rendering issues if the content isn't properly escaped or encoded. By defining the headers explicitly, you ensure that Thunderbird and other clients interpret the \r\n sequences as actual line breaks rather than literal text characters.
If you are migrating away from raw PHP mail functions entirely, frameworks like Laravel offer more robust tools for sending emails via SMTP, which handle encoding automatically. For instance, using Mail::raw() or the full Mailable classes ensures that line endings are preserved regardless of the client version. If your project requires a scalable solution without managing headers manually, exploring packages available at https://laravelcompany.com can provide pre-configured email services that abstract these low-level protocol details away from you.
The key takeaway is that mail() is a legacy function that places the burden of formatting on the server environment and the client's interpretation. Explicitly setting headers and using standard CRLF sequences within double-quoted strings ensures compatibility across Thunderbird, Outlook, and web-based clients alike.