Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Line breaks disappear or show as text in a PHP mail() body

ErrorNew lines (\r\n) are not working in email body

Short 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.php
  2. 2Match what you see, then jump to its fix

    1. 1The email shows the characters \r\n or \n instead of a line breakThe newline is in a single-quoted stringFix
    2. 2Content-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. 3Plain-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

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 newline

2Fix

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.php

Confirmed by php.net

Did this fix it?

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.php

2Fix

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.php

Confirmed by php.net

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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

Email

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.