<form method="post" action="send_email.php">
<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="message">Message:</label>
<textarea id="message" name="message" required></textarea>
<button type="submit">Send</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form input
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$message = test_input($_POST["message"]);
// Validate form input
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format";
exit;
}
// Compose email message
$to = "youremail@example.com";
$subject = "New form submission";
$body = "Name: $name\nEmail: $email\nMessage:\n$message";
// Send email
if (mail($to, $subject, $body)) {
echo "Email sent successfully";
} else {
echo "Email sending failed";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>