📘 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 करें।
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।