How to get mails from html contact form
To get emails from an HTML contact form, you'll need to use a combination of HTML, CSS, and a server-side programming language like PHP. Here's a step-by-step guide to help you achieve this:
Step 1: Create an HTML form
Create an HTML file (e.g., contact.html
) with the following code:
<form action="sendmail.php" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea><br><br>
<input type="submit" value="Send">
</form>
This form has three input fields: name
, email
, and message
. The action
attribute specifies the PHP file that will process the form data.
Step 2: Create a PHP file to process the form data
Create a PHP file (e.g., sendmail.php
) with the following code:
<?php
// Check if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form data
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Validate the form data
if (!empty($name) &&!empty($email) &&!empty($message)) {
// Send the email using PHP's mail function
$to = "[email protected]"; // Replace with your email address
$subject = "Contact Form Submission";
$body = "Name: $name\nEmail: $email\nMessage: $message";
mail($to, $subject, $body);
// Display a success message
echo "Thank you for contacting us!";
} else {
// Display an error message
echo "Please fill out all fields!";
}
}
?>
This PHP file checks if the form has been submitted, gets the form data, validates it, and sends an email using PHP's mail
function. You'll need to replace [email protected]
with your actual email address.
Step 3: Upload the files to your server
Upload both the contact.html
and sendmail.php
files to your server. Make sure the sendmail.php
file is in the same directory as the contact.html
file.
Step 4: Test the form
Open the contact.html
file in a web browser and fill out the form. Click the "Send" button to submit the form. If everything is set up correctly, you should receive an email with the form data.
That's it! You now have a functional HTML contact form that sends emails to your server.