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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jsjd23
Forum Newbie
Posts: 3
Joined: Sun Jul 19, 2009 10:14 am

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

Post 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.";
}
?>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

You mean the \n character?
jsjd23
Forum Newbie
Posts: 3
Joined: Sun Jul 19, 2009 10:14 am

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

Post 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?
User avatar
Jammerious
Forum Commoner
Posts: 59
Joined: Sat Jun 27, 2009 11:30 am
Location: Slovenia (EU)

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

Post 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"
jsjd23
Forum Newbie
Posts: 3
Joined: Sun Jul 19, 2009 10:14 am

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

Post by jsjd23 »

That I went with the first option and it works. Thanks so much!
Post Reply