Jelke
Jelke

Reputation: 55

Replace new lines with "\n" when read from URL

I am trying to send an email and the body of the email is a "GET" text from URL. This text is basically an email text that is in the format as given below:

Hi,

Nice to meet you.

Best regards.

However when copied to a URL it looks like this:

https://localhost/GetEmail.php?myBody=Hi,

Nice to meet you.

Best regards

I am not writing this body text, I am receiving it from a text file and this is the format I receive it in. And in my GetEmail.php script I am trying to add new line so ultimately it looks like a body email.

<?php
$myBody = htmlspecialchars($_GET['myBody']);
$bodyFinal = preg_replace('/[ \t]+/', ' ', preg_replace('/\s*$^\s*/m', "\n", $myBody));
..
..
?>

But unfortunately the final text looks like

Hi,Nice to meet you.Best regards

How do I fix this?

Upvotes: 1

Views: 133

Answers (2)

Elias
Elias

Reputation: 155

instead of GET use POST:

$myBody = htmlspecialchars($_POST['myBody']);

With post you doesnt send your data through the URL you use the HTTP-Request its recommended for bigger data/information

Upvotes: 1

Mohsen Nazari
Mohsen Nazari

Reputation: 1349

In email body there is no \n to be rendered. You can use the function nl2br() to convert all \n to html <br> tags and display it.

Upvotes: 0

Related Questions