New line in text area in html form is not saved

I have a text box in my contact form with php mail function. In php mail, I set the header to html.

But even if the user is of type type

Line1....
Line2....

I get mail like this.

Line1....Line2....

What could be the reason?

Update: The text area is simple.

<textarea id ="msg" name="message" cols="" rows="5" class="msg">Message</textarea>

Sent to this script with jquery ajax function

<?php
$sub =  "Message Posted";
$email = "some@somewhere.com";
$message = "<b>Message :</b><br/>" . $_REQUEST["msg"];
$message = wordwrap($message, 70);

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ifthi@ifthi.com' . "\r\n" .
    'Reply-To: '. $_REQUEST["email"] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


// Send
mail($email, $sub, $message,$headers);
?>

But when you receive an email, everyone is on the same line. Even if you write in 2 lines and send.

+3
source share
2 answers

if you set your mail to HTML, you must replace all line breaks with HTML and tags.

, PHP: nl2br ( $string [, bool $is_xhtml = true])

+2
<?php
//whatever you want to replace new lines with
$newLineCode = "<br/>";
$message = $_POST['myTextArea'] ; //unadulterad text we got via Post
$modifiedTextAreaText = ereg_replace( "\n", $newLineCode, $message);
echo " Result of Code Snippet " . $modifiedTextAreaText ;
?>
0

Source: https://habr.com/ru/post/1775118/


All Articles