How do you send user-entered line breaks from a form textarea to an email?

In case you haven’t noticed, with most PHP form processors, if a user fills out a textarea within your form using paragraphs or line breaks, the formatting of the paragraphs and line breaks are stripped away from your delivered e-mail contents.

In this example, we will use the PHP nl2br() function to fix this, displaying your form e-mail contents precisely as the user provided them.

What Does the PHP nl2br Function Do?

It’s straightforward really, the nl2br() function is used to insert HTML line breaks before any new lines within a string. In this case, we want to use the PHP nl2br() function and insert our form textarea field contents within that function to achieve paragraphs and line breaks when the form is submitted to us as an e-mail.

Let’s Look At Some Basic Code To Receive Your Textarea Field With The nl2br() function Included

The first order of business is our standard HTML textarea form field; for this example, we will name it “details” and this is how our form HTML will look:

<textarea placeholder="Details..." name="details" required></textarea> 

Now, within your PHP script to process your form to e-mail, we will need to grab the textarea contents, which will look like:

$details = $_REQUEST['details']; 

Next, for the contents of $details to be delivered with paragraphs and line breaks included based on how the user filled out the textarea we will need to add our nl2br() function. With the PHP from the above code included, this will look like:

$details = $_REQUEST['details'];$details = nl2br($details); 

There you have it; the above code places your textarea field contents within the PHP nl2br() function, which will now save the line breaks that your user has entered into the specific textarea.




  • The Power of Rate Limiting: Enhancing Website Traffic and Performance

    In the fast-paced digital landscape, where website traffic and user interactions are at an all-time high, ensuring optimal performance and security has become a critical concern for website administrators. One effective technique that can significantly improve both website traffic management and performance is rate limiting. Rate limiting is a method that controls the number of…

  • How do I Remove the “Save my name” Checkbox In WordPress?

    In WordPress, when users leave comments on your blog posts, they are often asked to provide their name, email address, and website. By default, WordPress includes a checkbox labeled “Save my name, email, and website in this browser for the next time I comment.” This checkbox allows users to have their information saved in cookies,…

  • How do you force SSL and fix mixed content with HTACCESS?

    So, you have a shiny new SSL certificate for your website, and now you need to force SSL, but you keep getting mixed content errors. This simple guide will consist of two parts and should get you up and running with a “green lock” in no time. Step 1 — You Need To Modify Your…

  • How do I reset all form fields using pure JavaScript?

    It doesn’t matter why you might want to add a reset form button for your users. It could be that the form has conditional logic, is super long, complex, or that it has form fields that are prone to user error. We will look at a straightforward way to add a form button to reset…

  • How do I create a simple news ticker using HTML and CSS?

    Alright, so you need some text, links, or images to scroll across your website from left to right. Some folks call this a “news ticker”, some call it a “text scroller” — we will stick with news ticker for the sake of keeping our references simple. Sure, you can make this happen with JavaScript but why complicate…

  • How do I make a textarea form field match an input form field?

    Have you ever wondered why the default font for a form textarea field is different when using a placeholder? The default fonts for all web forms are usually determined by the browser used, and technically, it is up to us to define what fonts or styles to display via our CSS. Here is how you…