Welcome to DVT IT Solutions' website. We have expertise in software development and website design.

Contact Form Using HTML and PHP

Contact Form Using HTML and PHP

  By DVT IT Solutions     15 Jul, 2014     10:47     1 Comments

Contact Form Using HTML and PHP

E-mails can easily be sent using HTML and the PHP mail() function. The syntax is:

mail($recipient_email, $subject, $message, $headers);

Let me use this opportunity to point out that using the PHP mail() function to send too many e-mails at a time to, for instance, Yahoo or GMail can lead to a permanent blockage of your server from sending e-mails to that e-mail server. This is because the e-mails will be detected as a spam or virus attack.

Let's get started. First of all create your HTML contact form as this:

<form action="send_email.php" method="post">
<label for="name">Name (required)</label><input name="name" placeholder="Name" />
<label for="email">Email (required)</label><input name="email" placeholder="E-mail"/>
<label for="subject">Subject (required)</label><input name="subject" placeholder="Subject"/>
<label for="message">Your Message (required)</label><textarea name="message" rows="8" cols="50" placeholder="Your Message"></textarea>
<input type="submit" name="send" value="Send" />
</form>

In your PHP file create a function to validate the email of the sender. The validation function

spamcheck($field)

will return true if the email is in the right email format and false if the email is not in the right email format.

function spamcheck($field) {
    //filter_var() sanitizes the e-mail
    //address using FILTER_SANITIZE_EMAIL
    $field = filter_var($field, FILTER_SANITIZE_EMAIL);
    //filter_var() validates the e-mail
    //address using FILTER_VALIDATE_EMAIL
    if (filter_var($field, FILTER_VALIDATE_EMAIL)) {
        return TRUE;
    } else {
        return FALSE;
    }
}

Now get the values from the contact form and use it to send the email.

//Assigning variables to the form values
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
//if all the fields are not empty then proceed to the validation of the email
//else display an error message to prompt the user to fill all fields
if ($name !== '' && $email !== '' && $subject !== '' && $message !== '') {
    //if email is filled out, proceed
    //check if the email address is invalid
    if (spamcheck($email) == FALSE) {
        die("<h3> :( Sending message failed. Enter a valid email.</h3>");
    } else {
        //send mail
        $to = 'dvtitsolutions@gmail.com';
        $message = $name . '\n' . $message;
        $headers = 'From: ' . $email;
        $send = @mail($to, $subject, $message, $headers);
        if ($send) {
            die('<h3> ;) Thank you. Email successfully sent.</h3>');
        } else {
            die('<h3> :( Sending email failed.</h3>');
        }
    }
} else {
    //if "email" is not filled out, kill the page with an error message
    die("<h3> :( Sending message failed. All fields are required.</h3>");
}

I preceeded the mail() function with the '@' symbol. This will hide any errors that will occur during the sending of the email and rather display the error message in the else block.

Feel free to comment. Peace!

1 Comments
Margherita Lach commented on Saturday, December 2nd, 2023 4:06:56 am

I would like to thank you for the efforts you've put in writing this website. I am hoping to see the same high-grade content from you in the future as well. In truth, your creative writing abilities has inspired me to get my own blog now ;)

Leave Your Comments
Share

Articles

  • How To Make A Bootable USB
    How To Make A Bootable USB

    You might be wondering how you going to make a fresh installation of Windows XP/Vista/7/8 on your notebook pc without a CD/DVD ROM.

    More...
  • Turn Your PC Into A Wireless Hotspot
    Turn Your PC Into A Wireless Hotspot

    In this tutorial, you will learn how to turn your Windows 7/8/8.1 PC into a wireless router using command prompt.

    More...
  • How To Install Whatsapp On Nokia Asha 200
    How To Install Whatsapp On Nokia Asha 200

    I believe you were and still feeling bad as I was about your Nokia Asha 200 not able to support WhatsApp but Nokia Asha 201 does.

    More...
  • Contact Form Using HTML and PHP
    Contact Form Using HTML and PHP

    PHP's in-built mail() function allows developers to send emails. This function only works if your server is configured to send emails.

    More...
  • How To Recover Lost Files
    How To Recover Lost Files

    This tutorial is for the one who has accidentally deleted files, formatted or lost a disk partition as a result of virus attack or disk failure.

    More...
  • How To Customize Your Disk Icon
    How To Customize Your Disk Icon

    This is one of the coolest things you can do on your PC. Customizing your hard disk icon or flash drive icon is very easy.

    More...
  • Reset or Remove Any Windows Password With Hiren's BootCD
    Reset or Remove Any Windows Password With Hiren's BootCD

    Hiren's BootCD is a first-aid tool which every computer wizard needs to have a copy either on CD or USB.

    More...
  • 10 Coding Techniques That Every Programmer Should Know
    10 Coding Techniques That Every Programmer Should Know

    This document addresses ten(10) fundamental coding techniques that improve the readability and maintainability of code.

    More...
Tags

Sign Up

Subscribe now and get the latest updates by DVT IT Solutions