Saturday, 12 October 2013

Sending Mail from Website Using PHP & HTML

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 redisgn it.

<!DOCTYPE html>
<html lang="en">
          <link rel="stylesheet" href="css/reset.css" type="text/css" media="all">

<div class="" >
                                    <form name="contact" method="post" action="sendmail.php" id="form1" style="float:left; padding:0px" >
                                    <h2>Book a Room</h2>
                                            <div class="row">
                                                <input type="text" class="input" name="name">
                                                Your Name:
                                            <div class="row">
                                                <input type="text" class="input" name="email">
                                                E-Mail Address:
                                            <div class="row">
                                                <input type="text" class="input" name="telephone">
                                                Contact No.:
                                            <div class="row">
                                                <input type="text" name="SelectedDate1" class="input" id="SelectedDate" readonly onClick="GetDate(this);" />
                                                Check In:
                                            <div class="row">
                                                <input type="text" name="SelectedDate2" class="input" id="Text1" readonly onClick="GetDate(this);" />
                                                Check Out:
                                            <div class="row">
                                                <input type="text" class="input" name="Place">
                                                Pick Up/Drop:
                                            <div class="row_textarea">
                                                Additional Comments:
                                                <textarea cols="1" rows="1" name="comments"></textarea>   
                                            <div class="wrapper">
                                                <a href="#" class="button1" onClick="document.getElementById('form1').submit()">Send</a>
                                                <a href="#" class="button1" onClick="document.getElementById('form1').reset()">Clear</a>

PHP Code:-

if(isset($_POST['email'])) {
    $email_to = "";
    $email_subject = "Booking Received";
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');     
    $name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
    $checkin=$_POST['SelectedDate1'];// required
    $checkout=$_POST['SelectedDate2'];// required
    $Place=$_POST['Place'];// required
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
   if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  if(strlen($error_message) > 0) {
    $email_message = "Form details below.\n\n";
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    $email_message .= "First Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
    $body="Mail Send";
    $msg='Name:-'. $_POST['name']."\n"
    .'Email:-'. $_POST['email']."\n"
    .'Contact No:-'.$_POST['telephone']."\n"
    .'Check In:-'.$_POST['SelectedDate1']."\n"
    .'Check Out:-'.$_POST['SelectedDate2']."\n"
  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>';
<!-- include your own success html here 
  echo "Your E-mail has been sent !Thank you for contacting us. We will be in touch with you very soon.";
   echo "E-mail sent was failed !";
   } -->
This is the PHP Code. You need to create a file and name it as sendmail.php and paste the PHP code.
and 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 you need to configure your SMTP server.
Thanks Any further suggestions are welcomed by you in comments.

No comments: