Page 1 of 1

Please help: I need new lines in a php contact form

Posted: Sun Jul 19, 2009 10:24 am
by jsjd23
Hi-
Here's my problem: The contact form is working, but when it sends an email all of the text runs together. How do you put new lines in between the name, email, phone, and message? I have spent hours trying to figure this out. I confess I have no PHP background, just learning as I go. Thanks for any help.

Code: Select all

<?php 
session_start();
if( $_SESSION['6_letters_code'] == $_POST['6_letters_code'] && !empty($_SESSION['6_letters_code'] ) ) {
   $to = "myemail@domain.com";
   $email_subject = "Website Inquiry";
$name = $_POST["name"];
$phone = $_POST["phone"];
$email = $_POST["email"];
$usermessage = $_POST["usermessage"];
$email_body = " You have received a new message from: " . $name . 
" Here is the message: " . $usermessage .
" Here is the phone:  " . $phone . 
" Here is the email:   " . $email;
 
   mail($to, $email_subject, $email_body);
 
   echo 'You entered the correct code. Your message was successfully emailed.';
} else {
echo "Sorry, you have provided an invalid security code. Please <a href='contact_form_with_captcha.html'>CLICK HERE</a> to try again.";
}
?>

Re: Please help: I need new lines in a php contact form

Posted: Sun Jul 19, 2009 10:41 am
by jackpf
You mean the \n character?

Re: Please help: I need new lines in a php contact form

Posted: Sun Jul 19, 2009 2:44 pm
by jsjd23
jackpf wrote:You mean the \n character?
Maybe? I've read that means new line, but I don't really know how or where to insert it. Can someone show me how it's done?

Re: Please help: I need new lines in a php contact form

Posted: Sun Jul 19, 2009 3:24 pm
by Jammerious
You can use "\n" (n for newline), however to be on the safe side you could use the PHP_EOL constant (EOL for end of line). This way PHP inserts the proper newline character, depending on which platform is it running.
In your example:

Code: Select all

$email_body = " You have received a new message from: " . $name . PHP_EOL .
 " Here is the message: " . $usermessage . PHP_EOL .
 " Here is the phone:  " . $phone . PHP_EOL .
 " Here is the email:   " . $email;
With \n this would be:

Code: Select all

$email_body = " You have received a new message from: " . $name . "\n" .
 " Here is the message: " . $usermessage . "\n" .
 " Here is the phone:  " . $phone . "\n" .
 " Here is the email:   " . $email;
Otherwise you can do newlines with \n without breaking and concatenating the string:

Code: Select all

$email_body = "Dear Santa,\nI am writing this letter to..."
I wonder if using \n inline could make a difference in parse times and such.
Oh one more thing, just noticed it, theres not need to break the string to insert the variable. Just use: " Here is the message: $usermessage \n"

Re: Please help: I need new lines in a php contact form

Posted: Sun Jul 19, 2009 4:41 pm
by jsjd23
That I went with the first option and it works. Thanks so much!