PHP Contact Form Email Sending Script (100% Working)
By using this code, you can get an email notification when someone submits the details to your contact form.
Instructions:
Please replace the emails with your email ID.
- Where do you want to receive the email? Replace myemail@gmail.com with your email ID.
- Use your hosting email ID. Like contact@provishal.com for www.provishal.com. Replace contact@website.com with your hosting domain email ID.
PHP Code:
<?php
if(isset($_POST['submit'])){
$to = "myemail@gmail.com"; // recipient email address
$from = "contact@website.com"; // 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
if(mail($to,$subject,$email_body,$headers)){
// 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>
</form>
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