Line break in textarea field PHP Mail form
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have a textarea in form and when the user writes the following text:


My order is:





I got all in one line

"Hi, My order is: bread milk biscuits juice"

I wish to keep every new line break that the user entered because when there is a lot of text in the textarea field, it is almost unreadable.

I will try to put nl2br somewhere in php code but didn't succeed. Because of my weak knowledge of php, please take the concrete steps I have to take.

Contact form:

PHP files:

Hi mashtullah, I don't use MySQL for this form.
Saaskoo 10 months ago
what database are you using?
mashtullah 10 months ago
Maybe I do not understand, but I should not set up any mySQL database in cPanel. Want to view some files?
Saaskoo 10 months ago
@Saasko yes if i can view the php code, will help
mashtullah 10 months ago
awarded to mashtullah

Crowdsource coding tasks.

4 Solutions

$subject = strip_tags($_POST['name']);
$message = nl2br(strip_tags($_POST['message']));
$from = strip_tags($_POST['email']);
$to = "";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $to. "\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);


Edit this line:

 $mail->Body = $letter;


 $mail->Body = nl2br(strip_tags($letter));
Do I have to put this code or change current? And where?
Saaskoo 10 months ago
I think you should change your current code, but I can't tell you where is wrong your code if you don't show me the code.
Stefano Balzarotti 10 months ago
No, I get plain mail instead html with lot of line breaks and empty spaces.
Saaskoo 10 months ago

Hi saasko,
i have created you the complete code with demo with a form to insert data and retrieve it in same format

you can see the code at my newly created git repo at following link:

base solution is written on 27 and 28 no line of above file link

//now use $description varriable in your email body

Winning solution

Here you go

on the file action.php on line 6, below the line if (!$_POST) exit; add this line

save and it should work perfectly
on the line 29 below this line $post = post_array_check($_POST); add this line
This should work now

@Saasko, try this out then let me know...
mashtullah 10 months ago
@Saasko, try again...
mashtullah 10 months ago
Thank you, I really appreciate your help! You won.
Saaskoo 10 months ago
@Saasko, Thanks mate...
mashtullah 10 months ago

without complexes here is your solution :p


textarea, define line breaks by "/r/n", but HTML didn't interpret that when reading it, and it handle it as a plain text.


the simple solution to fix this is, to replace the "/r/n" by the following "<br/>" which is the line break in HTML.

use this function and your problem will be fixed: str_replace("/r/n", "<br/>", $textarea);

Thanks for your time and tip, but @mashtullah just won.
Saaskoo 10 months ago
it's ok :) thanks for the reply ;)
Chlegou 10 months ago
Thank you for your participation :) Have a really nice day :)
Saaskoo 10 months ago
have a nice day too :)
Chlegou 10 months ago
View Timeline