We use cookies to enhance your experience. By continuing to visit this site you agree to our use of cookies. Learn More
contact@techquestworld.com
+919547614783
Premium Article
Published 5 months ago

Simple Contact Form in PHP with Email Functionality – Step-by-Step Guide

5 min read
87 views
Simple Contact Form in PHP with Email Functionality – Step-by-Step Guide
Photo by Techquestworld

Build a working contact form using PHP that sends form data via email. A great beginner PHP project to understand form handling and mail functions.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Stylish Contact Form</title>
<style>
* {
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

body {
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}

.contact-form {
background-color: #fff;
padding: 30px 40px;
border-radius: 15px;
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
width: 100%;
max-width: 400px;
}

.contact-form h2 {
text-align: center;
margin-bottom: 20px;
color: #333;
}

.contact-form input,
.contact-form textarea {
width: 100%;
padding: 12px 15px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 8px;
font-size: 16px;
transition: border 0.3s ease;
}

.contact-form input:focus,
.contact-form textarea:focus {
border-color: #2575fc;
outline: none;
}

.contact-form button {
width: 100%;
padding: 12px;
background-color: #2575fc;
color: white;
border: none;
font-size: 16px;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.3s ease;
}

.contact-form button:hover {
background-color: #1b5bd8;
}
</style>
</head>
<body>

<form class="contact-form" action="send.php" method="post">
<h2>Contact Us</h2>
<input type="text" name="name" placeholder="Your Name" required>
<input type="email" name="email" placeholder="Your Email" required>
<textarea name="message" placeholder="Your Message" rows="5"></textarea>
<button type="submit">Send Message</button>
</form>

</body>
</html>

send.php


<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST["name"]);
$email = htmlspecialchars($_POST["email"]);
$message = htmlspecialchars($_POST["message"]);

$to = "youremail@example.com";
$subject = "New Contact Message";
$body = "From: $name\nEmail: $email\nMessage:\n$message";

if (mail($to, $subject, $body)) {
echo "Message sent successfully!";
} else {
echo "Failed to send message.";
}
}
?>
Author
TAPAS SAHOO

Developer by Profession, Techie by Heart

A curious mind with a love for writing and technology, dedicated to simplifying web development and programming topics while keeping up with the ever-changing tech landscape.

Discussion (0)

Replying to
Previous Article
Build a To-Do List in JavaScript – Beginner F...

Develop a fully-featured To-Do app from scratch using only vanilla JavaScript—perfect for sharpening...

Next Article
Modern PHP Newsletter Subscription Form with...

A minimal PHP-powered newsletter form to collect email addresses, designed with a clean, modern look...

Related Articles

Modern PHP Newsletter Subscription Form with Stylish Design
PHP
Modern PHP Newsletter Subscription Form with Styli...

A minimal PHP-powered newsletter form to collect email addresses, designed with a clean, modern look...

Simple Feedback Form with PHP Email Support
PHP
Simple Feedback Form with PHP Email Support

Send feedback directly to your inbox using this beautifully designed PHP form.

Build a Complete PHP CRUD App with Bootstrap – No Framework
PHP
Build a Complete PHP CRUD App with Bootstrap – No...

Looking to build a full web app in PHP without relying on Laravel or CodeIgniter? This step-by-step...

Pessimistic vs Optimistic Locking in Laravel: A Developers Deep‑Dive
Pessimistic vs Optimistic Locking in Laravel: A De...

Learn how to safeguard your Laravel applications from race conditions by applying the right concurre...