Thursday, January 2, 2025
-0.4 C
London

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

Hot this week

Top 5 Yoga Asanas for a Stronger Core and Toned Belly

Introduction Yoga is a wonderful practice that not only helps...

How The Black Essentials Hoodie Became A Global Fashion Icon

The Black Essentials Hoodie is a wardrobe staple that...

The Benefits of Yoga for Women: Top 5 Popular Poses with Advantages and Risks

Yoga has been practiced for centuries and is known...

Why The Essentials Hoodie Is The New Standard In Streetwear

Essentials Hoodie has become a must-have item in fashion-conscious...

The History of SEO and How to Build a Career in Search Engine Optimization

Search Engine Optimization (SEO) is the practice of optimizing...

Topics

Top 5 Yoga Asanas for a Stronger Core and Toned Belly

Introduction Yoga is a wonderful practice that not only helps...

How The Black Essentials Hoodie Became A Global Fashion Icon

The Black Essentials Hoodie is a wardrobe staple that...

Why The Essentials Hoodie Is The New Standard In Streetwear

Essentials Hoodie has become a must-have item in fashion-conscious...

The History of SEO and How to Build a Career in Search Engine Optimization

Search Engine Optimization (SEO) is the practice of optimizing...

Building a Robust Software Testing Strategy for Your Development Team

In the fast-paced world of software development, ensuring that...

Online Cricket ID: Start your journey at Virat777

The Champion’s Trophy, the most awaited series and trophies...

Cyclamate Market Size, Share, Trends & Report | 2034

Cyclamate Market Outlook According to the report by Expert Market...

Related Articles

Popular Categories