Creating a Contact Form with PHP Mail Function

Date:

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>
<html>
<head>
    <title>Contact Form</title>
</head>
<body>
    <h2>Contact Us</h2>
    <form action="send_email.php" method="post">
        <label for="name">Name:</label>
        <input type="text" name="name" id="name" required>
        <br>

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

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

        <input type="submit" value="Send Message">
    </form>
</body>
</html>

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
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $to = "your_email@example.com"; // 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 your_email@example.com 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 Singhhttps://thestarbiznews.com
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:

Subscribe

spot_imgspot_imgspot_imgspot_img

Popular

More like this
Related

Mahadev’s Sacred Sanctuaries: Embarking on an Enlightening Spiritual Journey

भारत के आध्यात्मिक भूमि में विराजमान हैं कुछ सबसे...

Top Indoor Plants for Indian Homes: Easy Care and Beautiful Choices

Of course! Here are a few more Indian indoor...

Hanuman Chalisa: The Foundation of Devotion and Power

श्रीगुरु चरण सरोज रज निज मनु मुकुरु सुधारि।बरनउँ रघुबर...

Artificial Intelligence (AI): Shaping the Future of Technology

Unleashing the Potential of Artificial Intelligence Subheading: Artificial Intelligence (AI) stands...