PHP Contact Form Send Email

Search Engine Optimization

PHP Contact Form Send Email

03/25/2023 12:00 AM by Admin in Script and codes

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.

  • Where do you want to receive the email? Replace with your email ID.
  • Use your hosting email ID. Like for Replace with your hosting domain email ID.

PHP Code:

    $to = ""; // recipient email address
    $from = ""; // 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.