0

Is my code ok to send and email using PHP? Please take a look:

HTML:

<form class="contact_form" method="POST" action="mail.php">
    <div class="input">
        <label class="pl">Imię</label>
        <input id="name" type="text" name="name">
    </div>
    <div class="input">
        <label>E-mail</label>
        <input id="email" type="text" name="email">
    </div>
    <div class="input">
        <label class="pl">Temat</label>
        <input id="subject" type="text" name="subject">
    </div>
    <div class="message">
        <label class="pl">Twoja wiadomość</label>
        <textarea id="message" name="message"></textarea>
    </div>
    <div class="input">
        <input id="submit_pl" class="button pl" type="submit" value="Wyślij">
        <input id="reset_pl" class="button pl" type="reset" value="Resetuj">
    </div>
</form>

JS/JQuery:

// Form Validation ------------------------------------ //
  // Form is not valid on page load //
  var isFormValid = false;

  // input variables //
  var name = $('#name');
  var email = $('#email');
  var subject = $('#subject');
  var message = $('#message');
  var subtmitBtnPl = $('#submit_pl');
  var subtmitBtnEn = $('#submit_eng');
  var resetBtnPl = $('#reset_pl');
  var resetBtnEn = $('#reset_eng');

  var form = $('.contact_form');

  var isNameValid = function () {
    var nameVal = name.val();
    return nameVal.length > 0 && nameVal.length <= 100;
  };

  var isEmailValid = function () {
    var emailVal = email.val();
    return emailVal.indexOf('@') > -1 && emailVal.length <= 100;
  };

  var isSubjectValid = function () {
    var subjectVal = subject.val();
    return subjectVal.length > 0 && subjectVal.length <= 100;
  };

  var isMessageValid = function () {
    var messageVal = message.val();
    return messageVal.length > 0 && messageVal.length <= 500;
  };

  function formValidation() {
    var isFormValid = isNameValid() && isEmailValid() && isSubjectValid() && isMessageValid();

    if (isFormValid === false) {
      alert('error');
    } else {
      alert('succes');
      form[0].reset();
    }
  };

  form.submit(function(event) {
    event.preventDefault();
    formValidation();
  });

PHP:

<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['subject'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $body = $_POST['message'];
    $to = 'xyz@gmail.com';

    //headers
    $headers = "From: ".$name." <".$email.">\r\n";
    $headers .= "Reply-To: ".$email."\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset-utf-8";

    //send
    $send = mail($to, $subject, $body, $headers);
    if ($send) {
        echo '<br>';
        echo 'Thanks for contacting me. I will be with You shorty.';
    } else {
        echo 'error';
    }
}
?>

I'm very unexperienced with PHP. Could You please tell me why my contact form is not working/sending emails?

JQuery Validation works fine. This project is just on my drive not localhost (is it ok for PHP mail() to work?). Of course PHP file has name "mail.php".

aknosis
  • 3,602
  • 20
  • 33
svyatogor92
  • 113
  • 1
  • 2
  • 7
  • 1
    PHP needs to be installed to run PHP code. If it's on your drive (And you haven't set up some sort of PHP server), then no PHP will work. – Luke Sep 22 '17 at 03:51
  • besides that, is my code ok? What should i do to run PHP server with basic html/JS/PHP project? – svyatogor92 Sep 22 '17 at 03:55
  • @Luke is correct, if you're trying to do it on your computer (locally), I recommend installing xampp https://www.apachefriends.org/index.html – チャールズ Sep 22 '17 at 03:56
  • @チャールズ yeah, i figured it out and put my files in xampp htdocs folder. I manage to run it by localhost/ but it's still not working. Any sugestions why? – svyatogor92 Sep 22 '17 at 04:01
  • @svyatogor92 what is not working? Like, what's the error message? Is any of the PHP code displaying? Any error messages in your console? – チャールズ Sep 22 '17 at 04:03
  • @チャールズ Unfortunetly there is no message and code displayed. Like my PHP file was not executed at all. – svyatogor92 Sep 22 '17 at 04:06
  • Check https://stackoverflow.com/questions/15965376/how-to-configure-xampp-to-send-mail-from-localhost for ideas on how to configure sendmail on your computer – beaumontwebdev Sep 22 '17 at 04:07
  • @beaumontwebdev thanks, i will check it ;) I managed to sand and email using ajax service thanks to Your xampp advice :) I will read about this and fix it ;) Thanks a lot! ;) – svyatogor92 Sep 22 '17 at 04:20

1 Answers1

-1

install a web server like XAMPP,WAMPP, etc

rafon
  • 1,469
  • 11
  • 21
  • yeah, i figured it out and put my files in xampp htdocs folder. I manage to run it by localhost/ but it's still not working. Any sugestions why? – svyatogor92 Sep 22 '17 at 04:02
  • I believe you would be able to send email if you upload your files into a web hosting. I have a feeling that this sending email stuff is not working locally. – rafon Sep 22 '17 at 04:06
  • You mean by xampp localhost/ ? So i need to test it live? – svyatogor92 Sep 22 '17 at 04:08
  • I believe so, there are freehostings out there. One more thing, when you click the send button stuff, what does your network tab look like? we can check for status codes, etc.. – rafon Sep 22 '17 at 04:11
  • network says nothing about PHP. But i managed to send an email using ajax mail sending servise by xampp localhost/ so i call it sucess :) I will try to execute this PHP code on web hosting. Thanks for help :) – svyatogor92 Sep 22 '17 at 04:19