Contact Form to E-mail Linebreaks not working

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
jcmindeed
Forum Newbie
Posts: 4
Joined: Sat Jun 27, 2009 8:39 pm

Contact Form to E-mail Linebreaks not working

Post by jcmindeed »

I'm trying to make a contact form that is sent using PHP to an E-mail account. I can't seem to get it to work right.

Code: Select all

<?php
  $Firstname = $_REQUEST['FirstName'] ;
  $Lastname = $_REQUEST['LastName'] ;
  $Phone = $_REQUEST['Phone'] ;
  $Email = $_REQUEST['Email'] ;
  $Street = $_REQUEST['Street'] ;
  $City = $_REQUEST['City'] ;
  $State = $_REQUEST['State'] ;
  $Zip = $_REQUEST['Zip'] ;
  $Message = $_REQUEST['Message'] ;
  $Body = "First Name: $Firstname\n Last Name: $Lastname\n\n Phone: $Phone\n E-Mail: $Email\n\n Street: $Street\n City: $City\n State: $State\n Zip Code: $Zip\n\n Comments: $Message" ;
 
  if (!isset($_REQUEST['Email'])) {
    header( "Location: http://www.website.com/contact.html" );
  }
  
  if (empty($Email) || empty($Phone)){
    header( "Location: http://www.website.com/error.html" ); }
  
  else {
 
  mail( "name@example.com", "Contact Form Inquiry", $Body, "From: $Firstname <$Email>" );
  header( "Location: http://www.website.com/thankyou.html" );
  }
?>
 
As the code stands now, I won't even get an E-mail from the form. I've tried modifying it and the best I can get is for it to have the data without any line breaks. Any help would be appreciated. Thanks in advance.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: Contact Form to E-mail Linebreaks not working

Post by a94060 »

do an echo $Body first to see if the message is being formatted how you would like. If it is, then check if the mail function allowed from your server.
jcmindeed
Forum Newbie
Posts: 4
Joined: Sat Jun 27, 2009 8:39 pm

Re: Contact Form to E-mail Linebreaks not working

Post by jcmindeed »

Yeah when I tried echo with the \n it didn't format it the way I wanted it. Is their any other suggestions for formatting (making line breaks) without using \n?

I've also noticed that it won't go through if I use \n. However it does go through without all the \n. This is how it comes through now without the \n.
First Name: John Last Name: Doe Phone: 1112223333 E-Mail: johndoe@gmail.com Street: 123 Mulberry Lane City: Gumdrops State: XX Zip Code: 12345 Comments: Testing of form with content of body without formatting.
I want it to go through like this.
First Name: John
Last Name: Doe

Phone: 1112223333
E-Mail: johndoe@gmail.com

Street: 123 Mulberry Lane
City: Gumdrops
State: XX
Zip Code: 12345

Comments: Testing of form with content of body without formatting.
Any other advice? (Thanks for your reply though, appreciated.)
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: Contact Form to E-mail Linebreaks not working

Post by a94060 »

Code: Select all

 
  $Body = "First Name:". $Firstname. "\n Last Name:". $Lastname. "\n\n Phone:" . $Phone. "\n E-Mail: " .$Email. "\n\n Street:". $Street. "\n City: ".$City. "\n State:". $State. "\n Zip Code:". $Zip. "\n\n Comments: " .$Message" ;
 
 
I may have missed some periods. Also,I have not tested the code. if all else fails, you can use a <br> in it,which creates a line break. I personally never used the \n feature.
jcmindeed
Forum Newbie
Posts: 4
Joined: Sat Jun 27, 2009 8:39 pm

Re: Contact Form to E-mail Linebreaks not working

Post by jcmindeed »

Using <br> instead of \n actually just puts <br> into the text instead of formatting it. It seems though, using \r or \r\n works and formats it the way I want (using periods to space out the variables too like the way the code you posted above) and sends the message to my inbox. Thanks for your help!
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: Contact Form to E-mail Linebreaks not working

Post by a94060 »

alrighty. You are welcome. Thats surprising, the <br> was not with the variable right? It should have created a line break. Bot,you are welcome for whatever help i did give
jcmindeed
Forum Newbie
Posts: 4
Joined: Sat Jun 27, 2009 8:39 pm

Re: Contact Form to E-mail Linebreaks not working

Post by jcmindeed »

No, I put the <br> in like this.

Code: Select all

"First Name: ". $Firstname. "<br>Last Name: "
And came out like this.
First Name: John<br>Last Name:
Putting in \r\n or \r (where the <br> is at above) got it to work the way I wanted it too. <br> Might have worked if it was a HTML message, but it was just a plain text message. Oh well. Thanks again!
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Re: Contact Form to E-mail Linebreaks not working

Post by a94060 »

yea. I realized now as i got the notification in the inbox that it was an html tag, and you are sending a plaintext email.
Post Reply