By using this code, you can get an email notification when someone submits the details to your contact form.


Please replace the emails with your email ID.

PHP Code:

    $to = "[email protected]"; // recipient email address
    $from = "[email protected]"; // sender email address
    $subject = $_POST['subject'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $headers = "From:" . $from . "\r\n";
    $headers .= "Reply-To: " . $email . "\r\n";
    // build email body
    $email_body = "You have received a new message from your website contact form.\n\n";
    $email_body .= "Name: " . $name . "\n";
    $email_body .= "Email: " . $email . "\n";
    $email_body .= "Subject: " . $subject . "\n";
    $email_body .= "Message: " . $message . "\n";
    // send email
        // display success message
        echo "Message has been sent.";
    } else {
        // display error message
        echo "There was an error sending your message. Please try again later.";

<form method="post" action="">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" required>
    <label for="email">Email:</label>
    <input type="email" id="email" name="email" required>
    <label for="subject">Subject:</label>
    <input type="text" id="subject" name="subject" required>
    <label for="message">Message:</label>
    <textarea id="message" name="message" rows="5" required></textarea>
    <button type="submit" name="submit">Submit</button>

When a user submits the contact form, they will see a message saying “Message has been sent.” if the email is successfully sent, or “There was an error sending your message. Please try again later.” if there was a problem sending the email.

5/5 (3)