问题
I am using below code to send a textarea content as email using php
<?php
$to = $_POST['emailbox'] ;
$message1 = $_REQUEST['output_textarea'];
$subject = 'script';
$message = "
<html>
<body>
<table bgcolor='lightcyan'>
<p>heading</p>
$message1;
</table>
</body>
</html>
";
$headers = "From: admin@domain.com\r\n";
$headers .= "Reply-To: acr@domain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);
?>
But the email ignoring all line breaks in the textarea. How can I keep the line break in text area ?
回答1:
First of all, no semicolon needed here:
$message1;
As you use html for your email message, you have to use <br/>
tags to break lines.
Best way to replace \n
with <br/>
is use of nl2br() PHP function.
In your code replace:
$message1 = $_REQUEST['output_textarea'];
with:
$message1 = nl2br($_REQUEST['output_textarea']);
回答2:
You have to use it like this:
<?php
$message1 = $_REQUEST['output_textarea'];
$message1 = nl2br($message1);
?>
来源:https://stackoverflow.com/questions/18870131/linebreak-in-html-php-email