contact email form wont send with smtp

Viewed 21

I have created a contact form that is to be sent to a gmail address for testing purposes. However, after setting up sentmail for SMTP, the form I have created will not send.

I am now getting the message: Apache/2.4.51 (Win64) OpenSSL/1.1.1l PHP/7.4.26 Server at localhost Port 80.

I used the following link to set up the php.ini and sendmail.ini: https://websistent.com/using-sendmail-on-windows/

So the php.ini and sendmail.ini files have been set up and the following code is being used

        <form action="forms/contact.php" method="post" autocomplete="on">
          <div class="row">
            <div class="col-lg form-group">
              <label for="enter-name">Name:</label>
              <input type="text" id="enter-name" name="visitor_name" class="form-control"  placeholder="Your Name" pattern=[A-Z\sa-z]{3,20} required>
            </div>
            <div class="col-lg form-group ">
              <label for="enter-email">Email:</label>
              <input type="email" id="enter-email" name="visitor_email" class="form-control"  placeholder="Your Email" required>
            </div>
          </div>
          <div class="form-group ">
            <label for="enter-selection" class="pt-3 topics pb-2">Reason for contacting:</label><br />

            <select id="enter-selection" name="visitor_topic" required>
          <option value="" disabled selected>Select a Topic</option>                  

              <option value="experience">Restaurant Experience</option>
              <option value="to-go">To-go Experience</option>
              <option value="queries">Questions or Suggestions</option>
              <option value="technical support">Technical Help</option>
              <option value="other">Other</option>

           </select>
          </div>

       <div class="form-group">
       <label for="enter-message">Message:</label>
        <textarea class="form-control" id="enter-message" name="visitor_message" placeholder="Contact us..."></textarea>
        <div class="validate"></div>
      </div>
          <div class="text-center pb-3"><button type="submit">Send Message</button></div>
        </form><!--End of form-->

<?php
//the filter_var()function is used to validate or sanitize all types of user input.
//htmlspecialchars() function encodes all special HTML characters in the visitor message sent 
if($_POST) {
    $visitor_name = "";
    $visitor_email = "";
    $visitor_topic = "";
    $visitor_message = "";
    $email_body = "<div>";
      
    if(isset($_POST['visitor_name'])) {
        $visitor_name = filter_var($_POST['visitor_name'], FILTER_SANITIZE_STRING);
        $email_body .= "<div>
                           <label><b>Visitor Name:</b></label>&nbsp;<span>".$visitor_name."</span>
                        </div>";
    }
 
    if(isset($_POST['visitor_email'])) {
        $visitor_email = str_replace(array("\r", "\n", "%0a", "%0d"), '', $_POST['visitor_email']);
        $visitor_email = filter_var($visitor_email, FILTER_VALIDATE_EMAIL);
        $email_body .= "<div>
                           <label><b>Visitor Email:</b></label>&nbsp;<span>".$visitor_email."</span>
                        </div>";
    }
      
    if(isset($_POST['visitor_topic'])) {
        $visitor_topic = filter_var($_POST['visitor_topic'], FILTER_SANITIZE_STRING);
        $email_body .= "<div>
                           <label><b>Reason For Contacting Us:</b></label>&nbsp;<span>".$visitor_topic."</span>
                        </div>";
    }
      
   
    if(isset($_POST['visitor_message'])) {
        $visitor_message = htmlspecialchars($_POST['visitor_message']);
        $email_body .= "<div>
                           <label><b>Visitor Message:</b></label>
                           <div>".$visitor_message."</div>
                        </div>";
    }
      
    if($visitor_topic == "experience") {
        $recipient = "digi@gmail.com";
    }
    else if($visitor_topic == "to-go") {
        $recipient = "digi@gmail.com";
    }
     else if($visitor_topic == "queries") {
        $recipient = "digi@gmail.com";
    }
    else if($visitor_topic == "technical support") {
        $recipient = "digi@gmail.com";
    }
    else {
        $recipient = "digi@gmail.com";
    }
      
    $email_body .= "</div>";
 
    $headers  = 'MIME-Version: 1.0' . "\r\n"
    .'Content-type: text/html; charset=utf-8' . "\r\n"
    .'From: ' . $visitor_email . "\r\n";
      
    if(mail($recipient, $email_body, $headers)) {
        echo "<p>Thank you for contacting us, $visitor_name. You will get a reply within 24 hours.</p>";
    } else {
        echo '<p>We are sorry but the email did not send</p>';
    }
      
} else {
    echo '<p>Something went wrong</p>';
}
?>

Any help would be most appreciated as have tried for ages!

0 Answers
Related