Creating a Contact Form with PHP Mail Function


In this tutorial, we will learn how to create a contact form using HTML and PHP’s built-in mail function. The contact form will allow users to send emails directly from the website to a specified email address. This is a handy feature for any website that needs to collect user feedback, inquiries, or messages.

Step 1: HTML Form Let’s start by creating an HTML form for our contact page. Save this code in a file named contact_form.php:

htmlCopy code<!DOCTYPE html>
    <title>Contact Form</title>
    <h2>Contact Us</h2>
    <form action="send_email.php" method="post">
        <label for="name">Name:</label>
        <input type="text" name="name" id="name" required>

        <label for="email">Email:</label>
        <input type="email" name="email" id="email" required>

        <label for="message">Message:</label>
        <textarea name="message" id="message" rows="4" required></textarea>

        <input type="submit" value="Send Message">

Step 2: PHP Script to Send Email Create a PHP file named send_email.php to handle the form submission and send the email:

phpCopy code<?php
    $to = ""; // Replace with the email address where you want to receive messages
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];

    // Subject for the email
    $subject = "New Contact Form Submission from $name";

    // Compose the email body
    $body = "Name: $name\n";
    $body .= "Email: $email\n";
    $body .= "Message:\n$message";

    // Send the email using PHP's mail function
    if (mail($to, $subject, $body)) {
        echo "Thank you for your message. We will get back to you shortly!";
    } else {
        echo "Oops! Something went wrong. Please try again later.";

Step 3: Create the CSS (Optional) You can add some CSS to style your contact form and make it look more appealing.

Conclusion: Now you have a fully functional contact form that allows users to send emails directly from your website. By using PHP’s mail function, you can easily collect user messages and stay connected with your audience.

Note: The PHP mail function might not work correctly on all hosting environments. In production, you may consider using a more robust email library or a third-party email service to handle email sending reliably.

Remember to replace with your actual email address in the send_email.php file. With these steps, you have successfully created a basic contact form with email functionality for your website.

Shashi Prabha Singh
Shashi Prabha Singh
Shashi Prabha Singh is an accomplished author and expert in the field of marketing, technology, and the latest trends. With a passion for staying ahead of the curve, Shashi has dedicated her career to understanding and analyzing the ever-evolving landscape of marketing and technology.

Share post:




More like this

M3M Broadway Gurgaon

Discover the epitome of modern living at M3M Broadway...

Bridging Technologies: How RS485 Modbus Gateways Bridge Devices

In simple words, an RS485 Modbus gateway is a...

The Growing Scope of the Indian Space Industry

The space industry is a rapidly growing sector, with...

Benefits of Yoga Exercises: Flexibility, Strength, Posture, and More

Yoga exercise is a superb method to improve your...