tags:

views:

51

answers:

2

Beginner here.

I have a form with a textarea to return comments. I got a PHP script from helpvid.net to send the form to. So, the form is being sent to a PHP file.The form then returns the information to me in an email.

Problem is that the comments are sent as one large run-in paragraph. Even if the user hits Return in the field to make a new line, the text returned in the email is on one line. If the user hits Return twice to make a new paragraph, those paragraphs are returned as one long paragraph. I would like the text returned to have the line breaks that the user puts in.

Here is the code form the PHP file that is returning the comments:

$name = $_POST['name'];
$company = $_POST['company'];
$address = $_POST['address'];
$city = $_POST['city'];

$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$optin = $_POST['optin'];
$comments = $_POST['comments'];



$body = <<<EOD
<br><br>
Please send samples to: <br /><br />
$name <br />
$company <br />
$address <br>
$city, $state $zip <br><br />
Email: $email <br><br />
Opt-In to occasional email list?: $optin <br><br />
Comments: $comments <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);

Is there a way to modify this code, or my HTML file to return the paragraph breaks?

+3  A: 

nl2br is your friend:

$comments = nl2br($_POST['comments']);
timdev
:::ding! ding! ding! ding! ding!:::Yes! It works! Perfect! Thank you!!!!
JustBobF
+4  A: 

http://www.php.net/manual/en/function.nl2br.php

This should do it. Essentially it will turn line breaks into <br /> tags.

Note that if you are indeed sending plain text emails, this shouldn't be a problem. Are you sure your email client isn't stripping the line breaks? I know Outlook does this sometimes. Something worth checking...

UPDATE

Yes, the nl2br() will work in your case. But if you removed your original <br> tags and just used \n or hard line breaks you wouldn't need to use this function. The <br> tags are the only HTML in your email. It's not well-formed. You'd be better off removing them and the line $headers .= "Content-type: text/html\r\n"; so you just send a plain/text email.

Jason McCreary
I am using Apple's Mail.app; but, when I view the raw source, the only <br /> tags are those written in the PHP script, so there are no <br /> tags in the comments area. Anyway, the code posted by timdev works great!
JustBobF
See my update above.
Jason McCreary
Yes, Jason, that works, too. This version returns a space between paragraphs even when the user enters just one return stroke. So, timdev's solution sends email that looks most like what the user enters, in my case. And, with your method, I can indeed get rid of the <br /> characters in my PHP script.Many thanks to both of you!
JustBobF
No problem. `nl2br` is definitely the quick win. I just wanted you to see the difference between plain text email and HTML. For quick forms like this, HTML can become a headache.
Jason McCreary