Stijn Stijn - 20 days ago 8
PHP Question

How to send a form to own email using php?

I am making a website with a sign-up form containing four fields (username, email, password, password repeat). I want to run a validation experiment and therefore, when someone submits the form, I want the value in the email field to get send to my own email address so I can notice him when we go live. To realize this I made a html/bootstrap page with the form and a mailer.php page with the email instructions. Here is the signup form:

<div class="bs-example">
<div class="col-md-4 col-md-offset-4">
<form action="mailer.php" id="interest-form" method="POST">
<div class="form-group">
<input type="username" class="form-control" id="inputUsername" placeholder="Username">
</div>
<div class="form-group">
<input type="email" class="form-control" id="inputEmail" placeholder="Email">
</div>
<div class="form-group">
<input type="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
<div class="form-group">
<input type="password" class="form-control" id="inputPassword" placeholder="Repeat password">
</div>
<div class="checkbox">
<label><input type="checkbox">Remember me</label>
</div>
<button class='btn btn-signup btn-signup-sm btn-interest' id="interested" type="submit" name="remove_levels" value="delete">Sign up</button>
</form>
</div>
</div>


And here is some javascript I use to make a confirmation box pop-up asking the user if he wants to be notified when we launch and leave his email:

<script type="text/javascript">
$(document).ready(function(){
$('.btn').on('click', function(e){
e.preventDefault(); // this line is key to preventing page reload
// when user clicks submit button inside form
bootbox.confirm("Work on the game is still in progress. Submit the form and you will receive an email as soon as we launch.", function(result) {
("Confirm result: "+result);
if (result > 0){
$('#interest-form').submit();
};
});
});
});


When the person clicks yes to that message he is directed to a mailer.php page that looks like this:

<?php

function check_input($data, $problem='')
{
return $data;
}

function show_success($message)
{
print_r($message);
}

function show_error($myError)
{
print_r("error:".$myError);
}

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

$myemail = "infobetpitch@gmail.com";

$username = check_input($_POST['inputName'], "Username");
$email = check_input($_POST['inputEmail'], "Email");

$subject = "Someone has sent you a message\r\n";

$message = "Someone has sent you a message using your contac form: \r\n Username:".$username."\r\nEmail:".$email."\r\n";

mail($myemail, $subject, $message);

show_success($message);
exit();
}
?>


Now the signup form and the bootbox confirmation work but the problem lies in sending the email address to my email. When I click on the sign up button once I put the pages on the server I get a 405: method not allowed error:

Code: MethodNotAllowed
Message: The specified method is not allowed against this resource.
ResourceType: OBJECT
Method: POST
RequestId: 0565E2E8DB90C71B
HostId: cWkoCKGeVwAWRH/X3brKzv1tTnNuQxylIPm8kd9Fid8U0vWulYB2bJz/IgoqyRvNor

Or when I click on the link through my local browser I see a white page containing the exact php code I wrote. So I was wondering the following:


  • Is my mailer.php code written correctly or did I make some stupid mistake?

  • I host the website through Amazon S3. Is it perhaps a problem related to the server? Do I need to do something else before this will work?



I hope someone here can identify the problem because I'm really stuck.

Thanks in advance,

Stijn

Edit:

I changed the code according to the suggestion done by Ole but I still get the same error message. Now my code looks like this:

<div class="bs-example">
<div class="col-md-4 col-md-offset-4">
<form action="mailer.php" id="interest-form" method="POST">
<div class="form-group">
<input type="text" class="form-control" name="inputUsername" value="" placeholder="Username" />
</div>
<div class="form-group">
<input type="text" class="form-control" name="inputEmail" value="" placeholder="Email" />
</div>
<div class="form-group">
<input type="password" class="form-control" name="inputPassword" value="" placeholder="Password" />
</div>
<div class="form-group">
<input type="password" class="form-control" name="inputPassword" value="" placeholder="Password" />
</div>
<div class="checkbox">
<label><input type="checkbox">Remember me</label>
</div>
<button class='btn btn-signup btn-signup-sm' id="interested">Sign up</button>
</form>
</div>
</div>


And here is the code suggested by Ole:

<?php
if( $_SERVER['REQUEST_METHOD'] == "POST" ){

$myemail = "infobetpitch@gmail.com";

$username = $_POST['inputName'];
$email = $_POST['inputEmail'];

$subject = "Someone has sent you a message\r\n";

$message = "Someone has sent you a message using your contac form: \r\n Username:".$username."\r\nEmail:".$email."\r\n";

if( mail($myemail, $subject, $message) ){
echo "Mail is sent";
}
else {
echo "An error occured";
}

exit();
}
?>


I still can't get rid of that annoying error message and hope someone can help me spot my mistake.

Answer

You have several issues with your code here.

Issue #1: All of your input elements are missing name attributes. This is what post the value of the field to the $_POST array, i.e.:

<input type="text" name="thisistext" value="test" />

will create a $_POST array as following: $_POST['thisistext'] = "test"; So first off, you will have to put names to your inputs. This is basically what you have been using the id attribute for this far.

Issue #2: Although this isn't a direct issue, your PHP-code contains some unnecessary functions and returns success no matter what. Firstly. change the if( !isset( $_POST['submit'] ) ) and remove the use of check_input() unless you are planning to extend the function to actually do something. Id like to suggest you to use this instead:

<?php
    if( $_SERVER['REQUEST_METHOD'] == "POST" ){

        $myemail = "infobetpitch@gmail.com";

        $username = $_POST['inputName'];
        $email = $_POST['inputEmail'];

        $subject = "Someone has sent you a message\r\n";

        $message = "Someone has sent you a message using your contac form: \r\n Username:".$username."\r\nEmail:".$email."\r\n";

        if( mail($myemail, $subject, $message) ){
            echo "Mail is sent";
        }
        else {
            echo "An error occured";
        }

        exit();
    }
?>