Line breaks disappear or show as text in a PHP mail() body
New lines (\r\n) are not working in email bodyShort answer
Three things break line breaks in mail(): single-quoted strings, which do not turn \n into a newline; HTML mail, where a plain newline is only whitespace so you need <br> or nl2br(); and the line ending itself, since the manual asks for CRLF between lines.
Fix it fast
1Find out which cause you have
Look at how the message and the headers are built. A '\n' in single quotes means cause 1. A Content-type of text/html in the headers means cause 2.
grep -n "mail(\|Content-type\|\\\\n" send_mail.php2Match what you see, then jump to its fix
- 1
The email shows the characters \r\n or \n instead of a line breakThe newline is in a single-quoted stringFix - 2
Content-type: text/html is in the headers and the body shows as one paragraphThe mail is HTML, and a plain newline is just whitespaceFix - 3
Plain-text mail has odd breaks or doubled blank lines on one server onlyWrong line endings for the mail server, or lines that are too longFix
- 1
What does the error look like?
New lines (\r\n) are not working in email body
Cause 1 of 3
The newline is in a single-quoted string
1Check
In a single-quoted PHP string, the manual says escape sequences such as \r or \n are output literally. Only double-quoted strings turn them into control characters.
<?php
var_dump('a\nb'); // 4 characters, a backslash and an n
var_dump("a\nb"); // 3 characters, with a real newline2Fix
Use double quotes for the part that holds the newline.
<?php
$message = "Line 1\r\nLine 2\r\nLine 3";3Verify
Send a test mail to yourself. Each line should start on its own row.
php send_mail.phpConfirmed by php.net
Cause 2 of 3
The mail is HTML, and a plain newline is just whitespace
1Check
To send HTML mail the Content-type header must be set, and an HTML viewer does not show a raw newline as a break.
grep -n "text/html" send_mail.php2Fix
Convert newlines to HTML breaks with nl2br(), or write the markup with <br> and <p> yourself. Alternatively remove the text/html header and send plain text.
<?php
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$body = nl2br("Line 1\nLine 2", false);
mail($to, $subject, $body, $headers);3Verify
Open the received mail. The two lines should be on separate rows.
php send_mail.phpConfirmed by php.net
Cause 3 of 3
Wrong line endings for the mail server, or lines that are too long
1Check
The manual says each line of the message should be separated with CRLF (\r\n) and lines should not be larger than 70 characters. For a server that does not accept CRLF, the manual notes that some Unix mail transfer agents, qmail among them, convert LF to CRLF themselves.
<?php
var_dump(strpos($message, "\r\n") !== false);2Fix
Normalize the message to CRLF and wrap long lines with wordwrap(). Use bare LF only as a last resort, because the manual says it does not comply with RFC 2822.
<?php
$message = "Line 1\r\nLine 2\r\nLine 3";
$message = wordwrap($message, 70, "\r\n");
mail('user@example.com', 'My Subject', $message);3Verify
Send the test mail again and view the raw source of the received message.
php send_mail.phpConfirmed by php.net
How do I stop it coming back?
- Decide per mail: plain text with CRLF, or HTML with nl2br() or real markup. Do not mix them.
- Use double quotes whenever a string holds an escape sequence.
- Test every template by sending to yourself before real recipients.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
PHP mail() returns true but the email never arrives
Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing
mail() returning true only means the message was accepted for delivery, not that it arrived.
4 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix