Saturday, January 4, 2025
1.8 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

Understanding Casino Licenses: Why They Matter for Indian Players

If you're thinking of playing online casino games, you...

Reddy Anna ID Login: Discover Endless Betting Opportunities

Reddy Anna ID Login: Introduction Reddy Anna ID Login: a...

31st Convergence India Expo Showcases Technological Innovations

The 31st Convergence India Expo, held at Pragati Maidan...

Invest in Your Future with Land for Sale in Mangalagiri

Introduction Investing in real estate has long been considered one...

Makeup Artist in Bhopal: Elevating Beauty with Expertise and Style

In the vibrant city of Bhopal, where tradition meets...

Topics

Understanding Casino Licenses: Why They Matter for Indian Players

If you're thinking of playing online casino games, you...

Reddy Anna ID Login: Discover Endless Betting Opportunities

Reddy Anna ID Login: Introduction Reddy Anna ID Login: a...

31st Convergence India Expo Showcases Technological Innovations

The 31st Convergence India Expo, held at Pragati Maidan...

Invest in Your Future with Land for Sale in Mangalagiri

Introduction Investing in real estate has long been considered one...

Makeup Artist in Bhopal: Elevating Beauty with Expertise and Style

In the vibrant city of Bhopal, where tradition meets...

The Internet of Things (IoT): Revolutionizing Industries and Shaping the Future

With the rapid advancements in technology, the Internet of...

Related Articles

Popular Categories