send an email when a contact form was submitted php code example
Example 1: send email when form is submitted php
<?php
if(isset($_POST['submit'])){
$to = "[email protected]";
$from = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Example 2: how to connect contact form to email in php
<html>
<head>
<title>PHP Contact Form - The Email Method - Hyvor Developer</title>
</head>
<body>
<form method="POST" action="">
<div class="input-wrap">
<span class="label">Email:</span>
<input type="text" name="email">
</div>
<div class="input-wrap">
<span class="label">Message:</span>
<textarea name="message"></textarea>
</div>
<div class="input-wrap">
<input type="submit" name="submit" value="Submit" class="submit-button">
</div>
</form>
</body>
</html>