0

After having learned PHP recently, I am trying to implement my studies in a project.

I am in creating an extremely simple contact form (no validation for now) but it doesnt seem to be working in terms of sending the email.

Now I know that in order for this to work it needs to be uploaded to a server and I do so using netlify.

I have replaced the original email address with a dummy one for privacy reasons.

HTML:

<form class="joinContactForm" action="main.php" method="post">

  <div class="flexFormRow">
  <label class="formLabel" for="name">Your Name:</label> <input class="formInput" type="text" name="name" id="name"/>
</div>

  <div class="flexFormRow">
    <label class="formLabel" for="email">Your Email:</label> <input class="formInput" type="text" name="email" id="email"/>
  </div>

  <div class="flexFormRow">
    <label class="formLabel" for="number">Phone Number:</label> <input class="formInput" type="text" name="number" id="number"/>
  </div>

  <div class="flexFormRow">
     <label class="formLabel" for="age">Age:</label> <input class="formInput" type="text" name="age" id="age"/>
  </div>

  <div class="flexFormRow">
      <span class="formLabel2">Address:</span> <textarea name="address" id="address" class="addressText"> </textarea>
  </div>

  <div class="flexFormRow">
     <label class="formLabel" for="postcode">Postcode:</label> <input class="formInput" type="text" name="postcode" id="postcode"/>
  </div>

  <div class="flexFormRow">
      <span class="formLabel2" for="comments">Comments:</span> <textarea id="comments" name="comments" class="addressText"> </textarea>
  </div> 

  <div class="flexFormRow">
    <span class="formLabel2"></span><button type="submit" id="submit" name="submit" class="submitForm">SUBMIT</button>
  </div>

</form>

PHP:

<?php


      if (isset($_POST["submit"])){

        $name = $_POST["name"];
        $email = $_POST["email"];
        $number = $_POST["number"];
        $age = $_POST["age"];
        $address = $_POST["address"];
        $postcode = $_POST["postcode"];
        $comments = $_POST["comments"];

        $mailTo = "example@hotmail.co.uk";
        $mailFrom = "From ".$email;
        $mailText = "You have received an email from ".$name.".\n\n".$comments;

        mail($mailTo, $mailFrom, $mailText);


      }


     ?>

Thanks for the help!

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
CoderAz
  • 89
  • 4
  • 12
  • Here's a good general guide to debugging email problems in PHP: https://stackoverflow.com/a/24644450/5947043 – ADyson Jul 13 '20 at 10:54
  • P.S. it's a bad idea to let the user specify the "from" address. This can make the mails look like spam (as the domain in the from address and the domain from which the mail is physically being sent don't match, and this looks suspicious to a receiving mail server's spam filters). Just include their email address in the body of the mail, and use a fixed "from" address like `donotreply@yourdomain.com` where `yourdomain.com` matches the domain of the mailserver you're using. This may not be your only problem, but it's about the only thing I can definitely suggest just based on seeing your code. – ADyson Jul 13 '20 at 10:56
  • 2
    Does this answer your question? [How to run php files on netlify?](https://stackoverflow.com/questions/52042500/how-to-run-php-files-on-netlify) (in short, you can't run PHP on Netlify) – Joni Jul 13 '20 at 11:00
  • Thanks for the input @ADyson, fully appreciated – CoderAz Jul 13 '20 at 11:00

0 Answers0