-4

Ok so i made a HTML website for a school project and i tried incorporating into it a " Contact Us " section in which u give your name , email and a message to be sent to my adress. I can't seem to find any error into the code . When i am trying to send the info , instead of executing the php , it opens a new white Web Page and no mail is sent.

**That's how the Page looks like and what happens when i Submit : https://i.stack.imgur.com/zQB14.jpg ** ( as u can see the files are located in the same folder so that's not a problem )

That's my code HTML :

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Contact Page</title>
<link rel="stylesheet" href="style2.css" type="text/css" media="screen" />
<link rel="icon" href="icon2.png" type="image/x-icon"/>

</head>
<body>


<div class="container">
  <div style="text-align:center">
    <h2>Contact Us</h2>
    <p>Leave a message or a suggestion:</p>
  </div>
  <div class="row">
    <div class="column">
      <img src="gif3.gif" style="width:100%;">
    </div>
    <div class="column">
      <form id="column-form" method="post" action="column-form-handler.php">
        <label for="fullname">Full Name</label>
        <input type="text" id="fullname" name="fullname" placeholder="Your full name.." required>
        
        <label for="email">Email</label>
        <input type="email" id="email" name="email" placeholder="Your email adress.." required>
       
       <label for="country">Country</label>
        <select id="country" name="country" >
          <option value="australia">Australia</option>
          <option value="canada">Canada</option>
          <option value="usa">USA</option>
        </select>
        
        <label for="subject">Subject</label>
        <textarea id="subject" name="subject" placeholder="Write something.." style="height:170px"></textarea>
        <input type="submit" value="Submit">
      </form>
    </div>
  </div>
</div>

</body>
</html>

And here is the PHP :

<?php
     
    $name = $_POST['fullname'];
    $visitor_email = $_POST['email'];
    $message = $_POST['subject'];
    
    $email_from = 'abcdefgh@gmail.com';
    $email_subject = "Website Messages";
    $email_body = "User Name: $name.\n".
                     "User Email: $visitor_email.\n".
                       "User Message: $message.\n";
                       
    $to = "efghhieig@gmail.com";
    $headers = "From: $email_from \r\n";
    $headers = "Reply-To: $visitor_email \r\n";
    mail($to,$email_subject,$email_body,$headers);
    header("Location: contact.html");
    
    
    ?>
DeMoon
  • 21
  • 7
  • 3
    PHP doesn't get run when you use `file:///` for sites, you need a server so that it runs over HTTP – Chris Haas Apr 08 '21 at 19:31
  • @ChrisHaas is there not a way to open a php using local files if i have access to internet ? – DeMoon Apr 08 '21 at 19:38
  • 1
    RUN A LOCAL SERVER https://stackoverflow.com/questions/1678010/php-server-on-local-machine – epascarello Apr 08 '21 at 19:40
  • 2
    No there isn't, because when you load it via `file://` the script will not be executed by the PHP interpreter. You need a webserver running on your machine - e.g. Apache or IIS - which is configured to serve PHP files, and then you need to go to the page in your browser like `http://localhost/contact.html` so that when you submit the form, it would end up posting to `http://localhost/column-form-handler.php` (instead of `file://column-form-handler.php` or whatever). Whether you have access to the internet is or not is completely irrelevant to this. – ADyson Apr 08 '21 at 19:40
  • @DeMoon short answer: **no**. Little bit longer, you need to learn basics. – biesior Apr 08 '21 at 19:42

1 Answers1

0

When you load the pages into your browser via file://, the PHP script will not be executed by the PHP interpreter.

You need a webserver running on your machine - e.g. Apache or IIS - which is configured to serve PHP files, and then you need to go to the page in your browser like http://localhost/contact.html so that when you submit the form, it would end up posting to http://localhost/column-form-handler.php (instead of file://column-form-handler.php or whatever).

Doing this means that the webserver will pass requests for .php files to the PHP interpreter, which will execute them, and pass the result of executing the code (as opposed to just the raw source code) back to the webserver which then returns it to the client (i.e. browser).

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • i tried looking for a tutorial for XAMPP with Apache included and others but i can't seem to find anything useful to help me . Is there any good tutorial ? i've been starring at the program for some hours – DeMoon Apr 09 '21 at 11:33
  • A tutorial to demonstrate what, specifically? What are you having trouble with? Have you read all the relevant official documentation etc? P.S. Recommendations for such things are [specifically off-topic](https://stackoverflow.com/help/on-topic) on StackOverflow. – ADyson Apr 09 '21 at 11:57
  • i managed to open the Apache server and run localhost/contact.html , but when it executes the php there is still no mail sent – DeMoon Apr 09 '21 at 12:14
  • That's likely to be because you need to configure a mailserver. `mail()` relies on an underlying mailserver. If you test its return value in your code, you'll probably find it's returning `false` due to that not being configured. To be honest, you're probably better off using https://github.com/PHPMailer/PHPMailer to send emails because a) it's easier to work with than mail(), and b) it supports sending mail via a 3rd-party SMTP service such as Gmail, Oultook, or any other mail provider which supports SMTP connections (which is most of them.) – ADyson Apr 09 '21 at 12:24
  • If it turns out it actually returns `true`, implying that the mail has at least been queued for sending, of if you configure a 3rd party provider successfully via PHPMailer, yet find you still don't receive the email, then see this comprehensive guide to trying to fix email problems: https://stackoverflow.com/a/24644450/5947043 . There are any number of things which can go wrong - bear in mind that even if you can _send_ an email successfully, it still does not in any way guarantee that anyone will _receive_ it! – ADyson Apr 09 '21 at 12:26
  • e.g. spam filters will often block mail which looks "wrong" in some way - in your case you're using a gmail address as the "from" address, but if you did get a local mailserver up and running, it would be clear to any mailserver to which that message was sent that the email did not in fact come from gmail - and that's a big red spam flag, just to begin with. You'd be better to use PHPMailer to connect to gmail via SMTP and log in as that `abcdefgh@gmail.com` account, so that you're actually sending the message from the same mailbox as the "from" field claims it is. – ADyson Apr 09 '21 at 12:30