linebreak in html php email

谁说我不能喝 提交于 2021-02-05 08:47:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!