Page 1 of 1

Contact Form to E-mail Linebreaks not working

Posted: Sat Jun 27, 2009 8:49 pm
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.

Re: Contact Form to E-mail Linebreaks not working

Posted: Sun Jun 28, 2009 8:22 am
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.

Re: Contact Form to E-mail Linebreaks not working

Posted: Sun Jun 28, 2009 3:46 pm
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.)

Re: Contact Form to E-mail Linebreaks not working

Posted: Sun Jun 28, 2009 3:58 pm
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.

Re: Contact Form to E-mail Linebreaks not working

Posted: Sun Jun 28, 2009 4:24 pm
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!

Re: Contact Form to E-mail Linebreaks not working

Posted: Sun Jun 28, 2009 5:09 pm
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

Re: Contact Form to E-mail Linebreaks not working

Posted: Sun Jun 28, 2009 5:48 pm
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!

Re: Contact Form to E-mail Linebreaks not working

Posted: Sun Jun 28, 2009 6:00 pm
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.