W3docs

PHP new line break in emails

In PHP, you can use the built-in function "nl2br" to convert newline characters to HTML line breaks in emails.

In PHP, you can convert newline characters to HTML line breaks using the built-in nl2br() function. This is useful when rendering plain text content inside an HTML email. For example:

Example of using the built-in function "nl2br" to convert newline characters to HTML line breaks in emails in PHP

<?php

$email_body = "Hello,

This is a test email.

Regards,
John";

$email_body = nl2br($email_body);
echo $email_body;

This will convert the newline characters in the email body to HTML line breaks, so the email will display properly in the recipient's email client.

Alternatively, you can use \r\n or \n to insert new line breaks in an email body.

Example of using the "\r\n" or "\n" to insert new line breaks in email body in PHP

<?php

$email_body = "Hello,\r\n\r\nThis is a test email.\r\n\r\nRegards,\r\nJohn";
echo $email_body;

Note that \r\n (CRLF) is the standard line ending for email bodies per RFC 5322. Using only \n (LF) may cause formatting issues on some mail servers.

Please note: nl2br() is designed for HTML output. For plain text emails, always use \r\n or \n. If you are sending HTML emails, you can also use <br> or <p> tags directly.