0

I have an issue with PHP's mail() function. I'm not on the localhost, I'm not using a local server or something of this kind.

I'm just doing a plain website with HTML/CSS and I'm on Windows.

Basically, I made a contact form in HTML and I want it to send me an email on my Gmail address with the information filled by the user. I have re-used an old piece of PHP code that I already used before and that worked (one year ago).

However, when I fill the form and submit it (with the code below), I get nothing in my inbox.

Moreover, when I click on the send button ("J'envoie!"), it redirects me to the contactform.php file (a blank page where I see the php code of this file).

I checked that it was the right address, check if the name ("UName1", "UName2", "btn-send") matched the names I put in the PHP file but still, I can't find where is my error...

Can you please help me with that? I have no clue where the error is...

So here are my 2 files:

  • submit.html (the form part only)
<form class="part" action="contactform.php" method="post">

<br>

<div class="input-wrapper">
    <input type="text" name="UName1" placeholder="Mon nom"></input>
</div>

<br><br>

<div class="input-wrapper">
    <input type="text" name="UName2" placeholder="Le titre de mes notes"></input>
</div>

<br><br>

<a href="arigato.html" style="text-decoration: none;">
<button type="submit" name="btn-send" class="input-wrapper2">J'envoie!</button>     
</a>

</form>
  • contactform.php
<?php 

    if(isset($_POST['btn-send']))
    {
       $UserName = $_POST['UName1'];
       $UserAccount = $_POST['UName2'];
       
       

       if(empty($UserName) || empty($UserAccount))
       {
           header('location:submit.html?error');
       }
       else
       {
           $to = "santoshpassoubady@gmail.com";

           if(mail($to,$UserName,$UserAccount))
           {
               header("location:submit.html?success");
           }
       }
    }
    else
    {
        header("location:submit.html");
    }
?>

Please tell, if there is any other thing information I should give!

  • The built-in function `mail()` relies on the presence of external application which will actually send the message. Since you're on Windows - I assume you're using XAMPP. Here is a quick HOWTO for configuring your GMail account - http://www.arion.es/dashboard/docs/send-mail.html If you're not using XAMPP - then you will have to use the PHPMailer class which performs the low-level SMTP protocol and connects to GMail. – IVO GELOV Oct 26 '21 at 07:59
  • 3
    When you say "I'm not on localhost, I'm not using a local server", what _are_ you using to run the PHP code? If you're just double-clicking your HTML file in File Explorer (or whatever it's called now), I don't think that's going to work because you need a server somewhere to run PHP - it won't run in your browser like your HTML/CSS/JS does. From "a blank page where I see the php code of this file", I suspect that's your problem. – droopsnoot Oct 26 '21 at 08:02
  • @droopsnoot oh so what should I do? The thing is that I'm sure that it used to work before on my old project (because at that time I didn't know anything about localhost) – Santosh Passoubady Oct 26 '21 at 08:14
  • @IVOGELOV Hmm... no I've heard about XAMPP but I don't know what it is... What is the simplest way? Using PHPMailer? Because for the moment all I have is few HTML and CSS files on a folder. – Santosh Passoubady Oct 26 '21 at 08:15
  • 1
    _"I''ve heard about XAMPP but I don't know what it is"_ - Google is your friend here. Read up what it is and how it works. There are tons of guides out there. In short, you need to have a web server running with PHP support. Now you're opening the PHP files directly in the browser, but browsers (the clients) have no way of executing PHP code. PHP is a server side language. [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – M. Eriksson Oct 26 '21 at 08:23
  • @MagnusEriksson Okkk thanks, but then I would like to deploy the website online (certainly firebases) isn't there a way that it could do it with Firebase (have you ever heard about it)? Sorry for these noob questions... – Santosh Passoubady Oct 26 '21 at 08:28
  • 1
    If you Google around, you will quickly notice that Firebase is not a PHP interpreter - therefore it can not **execute** PHP code. – IVO GELOV Oct 26 '21 at 08:37
  • @IVOGELOV ok... thanks for guiding me, really appreciate that! – Santosh Passoubady Oct 26 '21 at 08:41

0 Answers0