Search

Creating an Email Form with PHP & HTML

0 views

PHP and
Prompt
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Email Form</title> </head> <body> <!-- Your form will go here --> </body> </html>

Prompt
<form action="process.php" method="post"> <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> <input type="submit" value="Send Message"> </form>

Here, the action attribute points to the PHP file that will process the form.

Prompt
<?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message'];

Prompt
$to = "recipient@example.com"; // replace with your email address $subject = "New message from $name"; $headers = "From: $email"; if (mail($to, $subject, $message, $headers)) { echo "Email sent successfully!"; } else { echo "Failed to send email."; } ?>

Prompt
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING); $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); $message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

Combining HTML and PHP empowers you to create functional email forms. Always prioritize security, and validate form data diligently.

For further learning on PHP and HTML, consider browsing Mozilla Developer Network's guide on HTML

Suggest a Correction

Found an error or have a suggestion? Let us know and we'll review it.

Share this article

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!