How to give line break in php mail
In PHP, when sending an email using the mail()
function or a mail library like PHPMailer, you can use the following methods to insert a line break:
-
Using
\r\n
:$message = "Hello,\r\nHow are you?"; mail("[email protected]", "Subject", $message);
The
\r\n
sequence is the standard way to insert a line break in plain text emails. -
Using
<br>
:$message = "Hello,<br>How are you?"; mail("[email protected]", "Subject", $message);
The
<br>
tag is an HTML tag that represents a line break. However, keep in mind that this will only work if the email client is set to render HTML emails. -
Using
nl2br()
:$message = "Hello,\nHow are you?"; $message = nl2br($message); mail("[email protected]", "Subject", $message);
The
nl2br()
function converts all newline characters (\n
) to HTML line breaks (<br>
). This is useful if you want to preserve the original line breaks in your email content. -
Using
PHP_EOL
:$message = "Hello,". PHP_EOL. "How are you?"; mail("[email protected]", "Subject", $message);
The
PHP_EOL
constant represents the end-of-line character for your operating system (e.g.,\r\n
on Windows,\n
on Unix-like systems).
Remember to adjust the email content according to the email client and recipient's preferences. Some email clients may not render line breaks correctly, so it's always a good idea to test your email content before sending it.