How do I preserve newlines when using filter_var(var, FILTER_SANITIZE_EMAIL)?

40 views Asked by At

I have a web form with a textarea for street address. The expectation is the user will enter two or three lines of text, hitting the Enter key for newlines within the textarea data.

When I submit the form, PHP sends the form data via mail(), with the related $_POST variable as part of the email body.

I'm sanitizing the user input with filter_var(), but in the resulting email the newlines seem to be getting stripped out of the textarea, and the lines end up being concatenated without even so much as a space between them.

How do I preserve newlines while still sanitizing the textarea input for email delivery?

Relevant HTML:

<textarea name="addr"></textarea>

Relevant PHP:

$em_body = 'Address:' . PHP_EOL . filter_var($_POST['addr'], FILTER_SANITIZE_EMAIL);
0

There are 0 answers