简体   繁体   English

如何使用php将表单发送到自己的电子邮件?

[英]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. 为了实现这一点,我制作了一个带有表单的html / bootstrap页面和一个带有电子邮件说明的mailer.php页面。 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: 这是一些我用来使确认框弹出的JavaScript,询问用户是否要在我们启动并留下他的电子邮件时收到通知:

<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: 当该人单击该消息的“是”时,他将被定向到如下所示的mailer.php页面:

<?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: 将页面放在服务器上后,单击“注册”按钮时,出现405:方法不允许错误:

Code: MethodNotAllowed Message: The specified method is not allowed against this resource. 代码:MethodNotAllowed消息:不允许对此资源使用指定的方法。 ResourceType: OBJECT Method: POST RequestId: 0565E2E8DB90C71B HostId: cWkoCKGeVwAWRH/X3brKzv1tTnNuQxylIPm8kd9Fid8U0vWulYB2bJz/IgoqyRvNor ResourceType:对象方法:POST请求ID:0565E2E8DB90C71B主机ID: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. 或者,当我通过本地浏览器单击链接时,我会看到一个包含我编写的确切php代码的白页。 So I was wondering the following: 所以我想知道以下几点:

  • Is my mailer.php code written correctly or did I make some stupid mistake? 我的mailer.php代码编写正确还是我犯了一些愚蠢的错误?

  • I host the website through Amazon S3. 我通过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. 我根据Ole的建议更改了代码,但仍然收到相同的错误消息。 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: 这是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. 我仍然无法摆脱那烦人的错误消息,希望有人能帮助我发现我的错误。

You have several issues with your code here. 您的代码在这里有几个问题。

Issue #1: All of your input elements are missing name attributes. 问题1:您所有的输入元素都缺少名称属性。 This is what post the value of the field to the $_POST array, ie: 这就是将字段的值发布到$_POST数组的原因,即:

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

will create a $_POST array as following: $_POST['thisistext'] = "test"; 将创建一个$_POST数组,如下所示: $_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. 到目前为止,这基本上就是您使用id属性的方式。

Issue #2: Although this isn't a direct issue, your PHP-code contains some unnecessary functions and returns success no matter what. 问题2:虽然这不是直接问题,但是您的PHP代码包含一些不必要的函数,无论如何都返回成功。 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. 更改if( !isset( $_POST['submit'] ) )并取消使用check_input()除非您打算扩展该功能以实际执行某项操作。 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();
    }
?>

In your file mailer.php change this: 在文件mailer.php中更改以下内容:

 $username = $_POST['inputName'];

to this : 对此:

 $username = $_POST['inputUsername'];

since the posted data from the form is under the inputUsername and not under the inputName. 因为从表单中发布的数据在inputUsername下,而不在inputName下。 If for example you had a field in your form eg 例如,如果您的表单中有一个字段,例如

<input type="text" class="form-control" name="example" value="" placeholder="Username" />

in order to get the data posted from the form in this field, you will need to get this with this php code 为了从该字段中的表单中获取数据,您将需要使用以下php代码获取此数据

$example = $_POST['example'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM