0

I'm a complete newbie in PHP but I have to use it to complete a project. I have to create a contact form in a website using WEMP. I followed this video: Video Link and fix his mistake that I saw in the comments. It wasn't working so I downloaded the send email and I changed the send email.ini to:

smtp_server=smtp.gmail.com   
smtp_port=465  
auth_username=mygmail@gmail.com  
auth_password=mypassword

then in php.ini:

SMTP = localhost
smtp_port = 25
sendmail_from ="myemail@gmail.com"

sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"

When I click on send it tells me that it is sent but I never receive it my email.

<?php

  // define variables and set to empty values
$name_error = $lname_error = $email_error = $city_error = $phone_error = "";
$name = $lname = $email = $phone = $message = $city = $success = "";

//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["first_name"])) {
    $name_error = "Name is required";
  } else {
    $name = test_input($_POST["first_name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $name_error = "Only letters and white space allowed"; 
    }
  }



  if (empty($_POST["last_name"])) {
    $lname_error = "Surname is required";
  } else {
    $lname = test_input($_POST["last_name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
      $lname_error = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $email_error = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Invalid email format"; 
    }
  }

  if (empty($_POST["phone"])) {
    $phone_error = "Phone is required";
  } else {
    $phone = test_input($_POST["phone"]);
    // check if e-mail address is well-formed
    if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
      $phone_error = "Invalid phone number"; 
    }
  }

  if (empty($_POST["city"])) {
    $city_error = "City is required";
  } else {
    $city = test_input($_POST["city"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
      $city_error = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["message"])) {
    $message = "";
  } else {
    $message = test_input($_POST["message"]);
  }

 if ($name_error == '' and $email_error == '' and $phone_error == '' and $lname_error == '' and $city_error == '' ){
      $message_body = '';
      unset($_POST['submit']);
      foreach ($_POST as $key => $value){
          $message_body .=  "$key: $value\n";
      }

      $to = 'myemail@gmail.com';
      $subject = 'Contact Form Submit';
      if (mail($to, $subject, $message_body)){
          $success = "Message sent, thank you for contacting us!";
          $name = $lname = $email = $phone = $city = $message ='';
      }
  }

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
 ?>

And this is the html in the other php file:

 <form id="contact" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" method="post">
    <h3>Contact</h3>
    <h4>Contact us today, and get reply with in 24 hours!</h4>
    <fieldset>
      <input placeholder="Your name" type="text" name="first_name" value="<?= $name?>" tabindex="1" autofocus>
      <span class="error"><?= $name_error ?></span>
    </fieldset>
        <fieldset>
      <input placeholder="Your surname" type="text" name="last_name" value="<?= $lname ?>" tabindex="1" autofocus>
      <span class="error"><?= $lname_error ?></span>
    </fieldset>
    <fieldset>
      <input placeholder="Your Email Address" type="text" name="email" value="<?= $email ?>" tabindex="2">
      <span class="error"><?= $email_error ?></span>
    </fieldset>
    <fieldset>
      <input placeholder="Your Phone Number" type="text" name="phone" value="<?= $phone ?>" tabindex="3">
      <span class="error"><?= $phone_error ?></span>
    </fieldset>
    <fieldset>
      <input placeholder="Your city" type="text" name="city" value="<?= $city ?>" tabindex="4" >
      <span class="error"><?= $city_error ?></span>
    </fieldset>
    <fieldset>
      <textarea value="<?= $message ?>" name="message" tabindex="5">
      </textarea>
    </fieldset>
    <fieldset>
      <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
    </fieldset>
    <div class="success"><?= $success ?></div>
  </form>

I will really appreciate any help! Thanks!

Zotov
  • 265
  • 1
  • 8
  • 20
  • And also when I put the Apache's SSL module on wamp stops working and needs to be reinstalled. – Zotov Jun 22 '17 at 03:55
  • Local servers can't send emails, if there are no errors then what you have should work when you put it on a live server. If anything you may have to plugin your live server's information. – SidTheBeard Jun 22 '17 at 04:05
  • You dont need Apache's ssl module, you probably need to activate `php_openssl.dll` – RiggsFolly Jun 22 '17 at 08:06

0 Answers0