How to Create a Contact Form with PHP

Post Date: February 13, 2019
Last Updated: October 07, 2026

Introduction

A contact form allows website visitors to send enquiries or messages directly to the website owner.

In this guide, we will explain how to create a simple contact form using HTML and PHP. The form will collect the visitor’s name, email address, and telephone number and send the submitted information to a specified email address.

Prerequisites

Before creating the contact form, make sure you have the following:

PHP-Enabled Web Server

Your website must be hosted on a server with PHP support.

Mail Configuration

The server must have a properly configured mail-sending service. Depending on your hosting environment, PHP’s mail() function may require additional mail server configuration.

Basic PHP and HTML Knowledge

Basic knowledge of HTML forms and PHP will help you understand and customize the example.

Implementation

Step 1: Create the Contact Form

Create an HTML file, such as contact.html, and add the following form:

<form name="contactform" method="post" action="send_form_email.php">
    <table width="1000px">
        <tr>
            <td valign="top">
                <label for="first_name">First Name *</label>
            </td>
            <td valign="top">
                <input type="text" name="first_name" maxlength="50" size="30" required>
            </td>
        </tr>

        <tr>
            <td valign="top">
                <label for="email">Email Address *</label>
            </td>
            <td valign="top">
                <input type="email" name="email" maxlength="80" size="30" required>
            </td>
        </tr>

        <tr>
            <td valign="top">
                <label for="telephone">Telephone Number</label>
            </td>
            <td valign="top">
                <input type="tel" name="telephone" maxlength="30" size="30">
            </td>
        </tr>

        <tr>
            <td colspan="2" style="text-align:center">
                <input type="submit" value="Submit">
            </td>
        </tr>
    </table>
</form>

The form submits the entered information to send_form_email.php using the POST method.

Step 2: Create the PHP Email Handler

Create a file named:

send_form_email.php

Add the following PHP code:

<?php

$email_to = "example@email.com";
$email_subject = "Enquiry Form";

$first_name = trim($_POST['first_name'] ?? '');
$email_from = trim($_POST['email'] ?? '');
$telephone = trim($_POST['telephone'] ?? '');

if ($first_name === '' || $email_from === '') {
    exit('Please complete all required fields.');
}

if (!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
    exit('Please enter a valid email address.');
}

$first_name = htmlspecialchars($first_name, ENT_QUOTES, 'UTF-8');
$telephone = htmlspecialchars($telephone, ENT_QUOTES, 'UTF-8');

$email_message  = "First Name: " . $first_name . "\n";
$email_message .= "Email: " . $email_from . "\n";
$email_message .= "Telephone: " . $telephone . "\n";

$headers  = "From: " . $email_from . "\r\n";
$headers .= "Reply-To: " . $email_from . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();

if (mail($email_to, $email_subject, $email_message, $headers)) {
    echo "Thank you for contacting us.";
} else {
    echo "Unable to send your message. Please try again later.";
}

?>

Replace:

example@email.com

with the email address where you want to receive the enquiries.

Step 3: Configure the Email Address

Update the following line:

$email_to = "example@email.com";

For example:

$email_to = "sales@domain.com";

The submitted form details will then be sent to the configured address.

Step 4: Test the Contact Form

Open the contact form in your browser and enter sample information.

For example:

  • First Name: John
  • Email Address: john@example.com
  • Telephone Number: +1 1234567890

Click Submit and verify that the message is received at the configured email address.

Step 5: Verify Server Mail Configuration

If the form submits successfully but the email is not received, the issue may be related to the server’s mail configuration rather than the PHP form itself.

Check your server’s mail configuration and mail logs, and verify that outbound email is permitted.

For production websites, using an authenticated SMTP service is generally more reliable than relying directly on PHP’s mail() function.

Security Considerations

A production contact form should include additional protections against abuse.

Validate User Input

Always validate submitted data on the server side. Do not rely only on HTML validation.

Protect Against Spam

Consider adding CAPTCHA or another anti-spam mechanism to prevent automated submissions.

Avoid Email Header Injection

Never directly trust user-supplied values in email headers. Validate email addresses before using them.

Use SMTP for Production

For reliable transactional email delivery, consider using an authenticated SMTP service rather than depending entirely on the server’s PHP mail configuration.

Conclusion

Creating a basic PHP contact form requires an HTML form to collect visitor information and a PHP script to process the submission and send the email.

The example above provides a simple starting point that can be customized for business enquiries, support requests, lead-generation forms, and other website communication requirements.

For production environments, additional security, spam protection, validation, and reliable SMTP configuration should be implemented.

Frequently Asked Questions

Can I use this PHP contact form on any website?

You can use it on websites hosted on servers that support PHP and have a properly configured email-sending service.

Why is the form not sending emails?

The PHP code may be working correctly while the server’s mail service is not configured properly. Check your mail server configuration and email logs.

Can I change the recipient email address?

Yes. Change the value of $email_to in the PHP script:

$email_to = "your-email@example.com";

Can I add more fields to the form?

Yes. You can add fields such as company name, subject, message, location, or other information and process them in the PHP script.

Should I use PHP mail() for a production website?

While mail() can be used in simple environments, an authenticated SMTP service is generally recommended for production websites because it provides better control over email delivery and authentication.

How can I protect the contact form from spam?

You can add CAPTCHA, rate limiting, server-side validation, honeypot fields, and other anti-spam controls depending on the requirements of your website.

Need Help with PHP Contact Forms?

Need help creating or configuring a PHP contact form for your website? Our team can assist with PHP development, form validation, SMTP configuration, email delivery, spam protection, security, troubleshooting, and ongoing website maintenance. Get in touch with our team here.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Scroll to Top