Home » Php » email – newline not working in PHP mail

email – newline not working in PHP mail

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m using the following to send an email:

<php ....
$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);
?>

When the message is received it does not start a new line at the “\r\n” but just prints them as part of the message.

I only tried it in Thunderbird 3, not any other clients.

How to&Answers:

Try to change your ' to " – php interprets a string inside single quotes as literals, whereas with quotes (") it will expand the \r\n to what you want.

More information: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

Answer:

Not an answer to the question, but may be of help to someone.

Send a HTML message, and instead of \n, use <BR>.

And use <PRE></PRE> or CSS for preformatted text, thus translating \n to actual new lines.

$headerFields = array(
    "From: [email protected]",
    "MIME-Version: 1.0",
    "Content-Type: text/html;charset=utf-8"
    );
mail($to, $subj, $msg, implode("\r\n", $headerFields));

Answer:

<?php ....
$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);
?>