PHP contact form

How can you create an email contact form using PHP?

With PHP, you can create an interface for sending mails. In example mentioned below we have created an form through which a user can send a mail: Code


<html>
  <body>
    <?php
    if (isset($_POST["btnSent"]) && isset($_POST["txtEmailId"]))
    {
      $email = $_POST["txtEmailId"] ;
      $subject = $_POST["txtSubject"] ;
      $message = $_POST["txtMessage"] ;
      mail("abc@example.com","Subject:$subject",$message,"From:$email");
      echo "Mail Successfully Sent.";
    }
    else
    {
    ?>
      <form method="post" id="frmMailInterface" name="frmMailInterface">
        Email: <input name="txtEmailId" type="text" />
        <br />
        Subject: <input name="txtSubject" type="text" />
        <br />
        Message:
        <br />
        <textarea name="txtMessage" rows="15" cols="40">
        </textarea>
        <br />
        <input type="submit" name="btnSent" id="btnSent" value="Send"/>
      </form>
    <?php
    }
    ?>
  </body>
</html>
In above mentioned example firstly a form will appear on the screen which will be requiring a email id of the receiver, subject of the mail and main message of your mail. And a send button when you click on send button after filling the required information then a message will "Mail Successfully Sent." and form will get hide.

FAQ 1. Why is my PHP contact form not sending emails? A PHP contact form may fail to send emails because of incorrect mail server configuration, invalid headers, server restrictions or an improperly configured mail transport. Check your PHP configuration and mail server logs to identify the issue. 2. Is PHP mail() enough for creating a contact form? Yes, the mail() function can handle basic email sending, provided the server has a configured mail transport. For production applications, PHPMailer with authenticated SMTP is generally recommended.