Sending Mail From Website Using PHP & HTML

Introduction

 
Here is the HTML Page Code
 
You can change the design of the HTML page since I am not providing you the CSS code of my page so you need to redesign it.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <title></title>  
  5.     <link rel="stylesheet" href="css/reset.css" type="text/css" media="all">  
  6.     <body>  
  7.         <div class="">  
  8.             <form name="contact" method="post" action="sendmail.php" id="form1" style="float: left;  
  9.             padding: 0px">  
  10.             <h2>  
  11.                 Book a Room</h2>  
  12.             <fieldset>  
  13.                 <div class="row">  
  14.                     <input type="text" class="input" name="name">  
  15.                     Your Name:  
  16.                 </div>  
  17.                 <div class="row">  
  18.                     <input type="text" class="input" name="email">  
  19.                     E-Mail Address:  
  20.                 </div>  
  21.                 <div class="row">  
  22.                     <input type="text" class="input" name="telephone">  
  23.                     Contact No.:  
  24.                 </div>  
  25.                 <div class="row">  
  26.                     <input type="text" name="SelectedDate1" class="input" id="SelectedDate" readonly  
  27.                         onclick="GetDate(this);" />  
  28.                     Check In:  
  29.                 </div>  
  30.                 <div class="row">  
  31.                     <input type="text" name="SelectedDate2" class="input" id="Text1" readonly onclick="GetDate(this);" />  
  32.                     Check Out:  
  33.                 </div>  
  34.                 <div class="row">  
  35.                     <input type="text" class="input" name="Place">  
  36.                     Pick Up/Drop:  
  37.                 </div>  
  38.                 <div class="row_textarea">   
Additional Comments
  1. <textarea cols="1" rows="1" name="comments"></textarea>  
  2. </div>  
  3. <div class="wrapper">  
  4.     <a href="#" class="button1" onclick="document.getElementById('form1').submit()">Send</a>  
  5.     <a href="#" class="button1" onclick="document.getElementById('form1').reset()">Clear</a>  
  6. </div>  
  7. </fieldset> </form> </div> </form> 
PHP Code
  1. <?php  
  2. if (isset($_POST['email']))  
  3. {  
  4.     // EDIT THE 2 LINES BELOW AS REQUIRED  
  5.     $email_to = "hotelflightpath@gmail.com";  
  6.     $email_subject = "Booking Received";  
  7.     function died($error)  
  8.     {  
  9.         // your error code can go here  
  10.         echo "We are very sorry, but there were error(s) found with the form you submitted. ";  
  11.         echo "These errors appear below.<br /><br />";  
  12.         echo $error . "<br /><br />";  
  13.         echo "Please go back and fix these errors.<br /><br />";  
  14.         die();  
  15.     }  
  16.     // validation expected data exists  
  17.     if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments']))  
  18.     {  
  19.         died('We are sorry, but there appears to be a problem with the form you submitted.');  
  20.     }  
  21.     $name = $_POST['name']; // required  
  22.     $email_from = $_POST['email']; // required  
  23.     $telephone = $_POST['telephone']; // not required  
  24.     $comments = $_POST['comments']; // required  
  25.     $checkin = $_POST['SelectedDate1']; // required  
  26.     $checkout = $_POST['SelectedDate2']; // required  
  27.     $Place = $_POST['Place']; // required  
  28.     $error_message = "";  
  29.     $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';  
  30.     if (!preg_match($email_exp$email_from))  
  31.     {  
  32.         $error_message .= 'The Email Address you entered does not appear to be valid.<br />';  
  33.     }  
  34.     $string_exp = "/^[A-Za-z .'-]+$/";  
  35.     if (!preg_match($string_exp$name))  
  36.     {  
  37.         $error_message .= 'The First Name you entered does not appear to be valid.<br />';  
  38.     }  
  39.     if (strlen($comments) < 2)  
  40.     {  
  41.         $error_message .= 'The Comments you entered do not appear to be valid.<br />';  
  42.     }  
  43.     if (strlen($error_message) > 0)  
  44.     {  
  45.         died($error_message);  
  46.     }  
  47.     $email_message = "Form details below.\n\n";  
  48.     function clean_string($string)  
  49.     {  
  50.         $bad = array(  
  51.             "content-type",  
  52.             "bcc:",  
  53.             "to:",  
  54.             "cc:",  
  55.             "href"  
  56.         );  
  57.         return str_replace($bad""$string);  
  58.     }  
  59.     $email_message .= "First Name: " . clean_string($name) . "\n";  
  60.     $email_message .= "Email: " . clean_string($email_from) . "\n";  
  61.     $email_message .= "Telephone: " . clean_string($telephone) . "\n";  
  62.     $email_message .= "Comments: " . clean_string($comments) . "\n";  
  63.     $body = "Mail Send";  
  64.     $msg = 'Name:-' . $_POST['name'] . "\n" . 'Email:-' . $_POST['email'] . "\n" . 'Contact No:-' . $_POST['telephone'] . "\n" . 'Check In:-' . $_POST['SelectedDate1'] . "\n" . 'Check Out:-' . $_POST['SelectedDate2'] . "\n" . 'Place:-' . $_POST['Place'] . "\n" . 'cOMMENTS:-' . $_POST['comments'];  
  65.     mail($email_to$email_subject$msg);  
  66.     echo '<span style="color:#FFFFFF">Your E-mail has been sent !Thank you for contacting us. We will be in touch with you very soon.</span>';  
  67. ?>  
  68. <!-- include your own success html here  
  69. if($send_email)  
  70. {  
  71. echo "Your E-mail has been sent !Thank you for contacting us. We will be in touch with you very soon.";  
  72. }  
  73. else  
  74. {  
  75. echo "E-mail sent was failed !";  
  76. } -->  
  77. <?php  
  78. }  
  79. ?> 
This is the PHP Code. You need to create a file and name it as "sendmail.php" and paste the PHP code.
 
Then create another page "contact.html" and paste the HTML code there. It will for the web server but if you need to run it on localhost then you need to configure your SMTP server.
 
Thanks. Any further suggestions by you are welcomed in the comments.