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

<!DOCTYPE html>

<html lang="en">

<head>

    <title></title>

    <link rel="stylesheet" href="css/reset.css" type="text/css" media="all">

    <body>

        <div class="">

            <form name="contact" method="post" action="sendmail.php" id="form1" style="float: left;

            padding: 0px">

            <h2>

                Book a Room</h2>

            <fieldset>

                <div class="row">

                    <input type="text" class="input" name="name">

                    Your Name:

                </div>

                <div class="row">

                    <input type="text" class="input" name="email">

                    E-Mail Address:

                </div>

                <div class="row">

                    <input type="text" class="input" name="telephone">

                    Contact No.:

                </div>

                <div class="row">

                    <input type="text" name="SelectedDate1" class="input" id="SelectedDate" readonly

                        onclick="GetDate(this);" />

                    Check In:

                </div>

                <div class="row">

                    <input type="text" name="SelectedDate2" class="input" id="Text1" readonly onclick="GetDate(this);" />

                    Check Out:

                </div>

                <div class="row">

                    <input type="text" class="input" name="Place">

                    Pick Up/Drop:

                </div>

                <div class="row_textarea">  

Additional Comments

<textarea cols="1" rows="1" name="comments"></textarea>

</div>

<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>

</div>

</fieldset> </form> </div> </form>

PHP Code

<?php

if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "[email protected]";

$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 />";

die();

}

// 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) {

died($error_message);

}

$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"

.'Place:-'.$_POST['Place']."\n"

.'cOMMENTS:-'.$_POST['comments'];

mail($email_to,$email_subject,$msg);

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

if($send_email)

{

echo "Your E-mail has been sent !Thank you for contacting us. We will be in touch with you very soon.";

}

else

{

echo "E-mail sent was failed !";

} -->

<?php

}

?>  

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.

Up Next
    Ebook Download
    View all
    Learn
    View all