Using PHP to send a reply from a form to my Email and is giving error 405 Not Allowed

Asked

Viewed 186 times

0

Every time I click to submit the form gives this error! How can I resolve this?

HTML 5 Form Code :

<form class = "col s12 m4 offset-m4 l4 offset-l4" action="submit_form.php" method="post">

    <div class="row">

        <div class="input-field col s12">
            <i class="material-icons prefix sKtq-scColor">&#xE87C;</i>
            <input id="icon_prefix" name="Nome" type="text" class="validate sKtq-scColor" required>
            <label for="icon_prefix" class = "sKtq-scColor">Nome</label>
        </div>

        <div class="input-field col s12">
            <i class="material-icons prefix sKtq-scColor">&#xE0BE;</i>
            <input id="icon_prefix" name="Email" type="email" class="validate sKtq-scColor" required>
            <label for="icon_prefix" class = "sKtq-scCo lor">Email</label>
        </div>

        <div class="input-field col s12">
            <i class="material-icons prefix sKtq-scColor">sort</i>
            <input id="icon_prefix" name="Assunto" type="text" class="validate sKtq-scColor" required>
            <label for="icon_prefix" class = "sKtq-scColor">Assunto</label>
        </div>

        <div class="input-field col s12">
            <i class="material-icons prefix sKtq-scColor">chat_bubble</i>
             <textarea id="textarea1" name="Mensagem" class="materialize-textarea validate sKtq-scColor" required></textarea>
             <label for="textarea1" class = "sKtq-scColor">Mensagem</label>
        </div>

    </div>

    <br>

    <div class = "center-align">

        <button class="btn waves-effect waves-light sKtq-bgScColor" type="submit"> Enviar
            <i class="material-icons right">send</i>
        </button>

    </div>

</form>

PHP code that is in the file submit_form.php :

<?php

if(isset($_POST['email'])) {
    $email_to = "mtqr1@hotmailcom";
    $email_subject = "Mensagem do site";
    $name = $_POST['Nome']; // required
    $email_from = $_POST['Email']; // required
    $subject = $_POST['Assunto']; // required
    $message = $_POST['Mensagem']; // required

    function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
    }

    $email_message = "Form details below.\n\n";
    $email_message .= "Name : ".clean_string($name)."\n";
    $email_message .= "Email : ".clean_string($email_from)."\n";
    $email_message .= "Assunto : ".clean_string($subject)."\n";
    $email_message .= "Mensagem : ".clean_string($message)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
  <!-- include your own success html here -->

  <div class="feedback">Thank you for contacting us. We will be in touch with you very soon.</div>

  <?php
}
?>

Error image

Erro

  • Are you using Github pages? If yes it does not allow you to run a script on the server side. Which begs the question: how did you manage to put?

  • So I’m using Git Hub Pages and I haven’t found it anywhere in the documentation saying it doesn’t allow it I just left this script. php at the root of my project ! You know what’s wrong with this script ?

1 answer

1


In the github documentation, more specifically on What is Github Pages? it is clear that you cannot run a PHP script. Only static documents like html, css, and javascript are supported.

The full quote is this:

Github Pages is a Static site hosting service and doesn’t support server-side code such as, PHP, Ruby, or Python.

In Portuguese:

Github Pages is a static website hosting service and not supports server-side code such as PHP, Ruby or Python.

This means that when you make a POST request, it is blocked by default, returning error 405, since it has been set in the server settings, since there is no reason to allow this since only Estatic pages are served.

You can even access the file using a GET request (just enter the url when the error message appears), but it will return the contents in plain text of your file (nothing will be interpreted).

To be able to run your script some alternatives:

  • Buy a lodging (for the whole site or just for the specific script)
  • Or use a free hosting only for the email file (ai makes a link on the github page), although free hosting blocks email sending.
  • Sorry I hadn’t seen this but thanks for warning ! So I’ll try to find a way to send an E-mail using some Javascript API ! Thank you for your attention! Merry Christmas !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.