0

I'm trying to have my submit button send name, email and message to my desired address. When I hit send, i get a page that says "This page isn't working - HTTP error 405." Some notes: I'm using visual studio code and using their live server extension to test it. I have script tags in HTML linking it to my php file (not sure if this matters as it's already linked under form element?) The "contact.php" is my php file. I'm a noobie so I'm hoping I'm making a simple mistake.

// MY PHP

<?php

$userName = $_POST['name'];
$userEmail= $_POST['email'];
$message= $_POST['message'];

$to = "stackoverflowexample@gmail.com";
$body = "";

$body .= "From: ".$userName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";

mail($to,$body);

?>

// MY HTML

              <form action="contact.php" method="POST">
                

                <h2>Send Message</h2>
                <div class="inputbox">
                    <input type="text" name="name" required="required">
                    <span>Full Name</span>
                </div>

                <div class="inputbox">
                    <input type="text" name="email" required="required">
                    <span>Email</span>
                </div>

                <div class="inputbox">
                    <textarea required="required" name="message"></textarea>
                    <span>Type your Message...</span>
                </div>

                <div class="inputbox">
                    <input type="submit" name="" value="Send">
                </div>

            </form>
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • Does this answer your question? [How can I send an email using PHP?](https://stackoverflow.com/questions/5335273/how-can-i-send-an-email-using-php) – Vicky Mar 30 '21 at 07:14
  • 2
    `405` is "Method not allowed". This seems like a webserver configuration error, it's not allowing you to send a `POST` method. – Barmar Mar 30 '21 at 07:15
  • 2
    @RichPhp How would that solve a 405 error? – Barmar Mar 30 '21 at 07:15
  • You're missing arguments to the `mail()` function. `mail($to, $subject, $body, $headers)` – Barmar Mar 30 '21 at 07:16
  • @Barmar in above question he just missing one argument that the error he has. so i suggest a question which is work or completing using same mail() function. in above question the suggest one there is really nice explanation for use of mail() or other better mail library. that is the main reason i suggest this for above question – Vicky Mar 30 '21 at 10:42

1 Answers1

0
  • If you write both HTML and PHP code in the same file then you need to save that single file as contact.php.
  • Also in PHP code Please set the condition to avoid warning like Undefined index
  • Also mail function must have at least 3 parameters 1. to 2. Subject 3. Body

See below code

<?php

// set condition 
if(!empty($_POST)) {

$userName = $_POST['name'];
$userEmail= $_POST['email'];
$message= $_POST['message'];

$to = "stackoverflowexample@gmail.com";
$body = "";

$body .= "From: ".$userName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";


// set subject name as second param
mail($to,"test",$body);

}

?>
Maulik
  • 104
  • 7