📘 Lesson · Lesson 41
Contact Form with Email
Contact Form with Email
About
A contact form lets visitors send you a message by email. Here is a simple version using PHP's
mail() function.HTML Form
<form method="post">
<input name="name" placeholder="Name">
<input name="email" placeholder="Email">
<textarea name="message"></textarea>
<button type="submit">Send</button>
</form>
PHP Handler
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = htmlspecialchars($_POST["name"]);
$msg = htmlspecialchars($_POST["message"]);
$to = "you@site.com";
$subject = "New message from $name";
$headers = "From: " . $_POST["email"];
if (mail($to, $subject, $msg, $headers)) {
echo "Message sent!";
}
}
For Reliable Delivery
On shared hosting, mail() may go to spam. For reliable email, use PHPMailer with SMTP (Gmail/your host SMTP).
Summary
- mail($to, $subject, $message, $headers) sends a basic email.
- Sanitize input; use PHPMailer+SMTP for reliable delivery.
परिचय
Contact form visitors को आपको email से message भेजने देता है। यह रहा PHP के
mail() function वाला simple version।HTML Form
<form method="post">
<input name="name" placeholder="Name">
<input name="email" placeholder="Email">
<textarea name="message"></textarea>
<button type="submit">Send</button>
</form>
PHP Handler
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = htmlspecialchars($_POST["name"]);
$msg = htmlspecialchars($_POST["message"]);
$to = "you@site.com";
$subject = "New message from $name";
$headers = "From: " . $_POST["email"];
if (mail($to, $subject, $msg, $headers)) {
echo "Message sent!";
}
}
भरोसेमंद Delivery के लिए
Shared hosting पर mail() spam में जा सकता है। भरोसेमंद email के लिए PHPMailer को SMTP (Gmail/अपने host SMTP) के साथ use करें।
सारांश
- mail($to, $subject, $message, $headers) basic email भेजता है।
- Input sanitize करें; भरोसेमंद delivery को PHPMailer+SMTP use करें।