[英]How to send a form to own email using php?
我正在使用包含四个字段(用户名,电子邮件,密码,重复密码)的注册表单制作一个网站。 我想进行一个验证实验,因此,当有人提交表单时,我希望电子邮件字段中的值发送到我自己的电子邮件地址,以便我们上线时可以注意到他。 为了实现这一点,我制作了一个带有表单的html / bootstrap页面和一个带有电子邮件说明的mailer.php页面。 这是注册表格:
<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>
这是一些我用来使确认框弹出的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();
};
});
});
});
当该人单击该消息的“是”时,他将被定向到如下所示的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();
}
?>
现在注册表单和启动箱确认有效,但问题出在将电子邮件地址发送到我的电子邮件中。 将页面放在服务器上后,单击“注册”按钮时,出现405:方法不允许错误:
代码:MethodNotAllowed消息:不允许对此资源使用指定的方法。 ResourceType:对象方法:POST请求ID:0565E2E8DB90C71B主机ID:cWkoCKGeVwAWRH / X3brKzv1tTnNuQxylIPm8kd9Fid8U0vWulYB2bJz / IgoqyRvNor
或者,当我通过本地浏览器单击链接时,我会看到一个包含我编写的确切php代码的白页。 所以我想知道以下几点:
我的mailer.php代码编写正确还是我犯了一些愚蠢的错误?
我通过Amazon S3托管网站。 可能是与服务器有关的问题? 我需要做其他事情才能起作用吗?
我希望这里的人能够找出问题所在,因为我真的被卡住了。
提前致谢,
斯蒂金
编辑:
我根据Ole的建议更改了代码,但仍然收到相同的错误消息。 现在我的代码如下:
<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>
这是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();
}
?>
我仍然无法摆脱那烦人的错误消息,希望有人能帮助我发现我的错误。
您的代码在这里有几个问题。
问题1:您所有的输入元素都缺少名称属性。 这就是将字段的值发布到$_POST
数组的原因,即:
<input type="text" name="thisistext" value="test" />
将创建一个$_POST
数组,如下所示: $_POST['thisistext'] = "test";
因此,首先,您将必须在输入中输入名称。 到目前为止,这基本上就是您使用id属性的方式。
问题2:虽然这不是直接问题,但是您的PHP代码包含一些不必要的函数,无论如何都返回成功。 首先。 更改if( !isset( $_POST['submit'] ) )
并取消使用check_input()
除非您打算扩展该功能以实际执行某项操作。 我想建议您改用以下方法:
<?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();
}
?>
在文件mailer.php中更改以下内容:
$username = $_POST['inputName'];
对此:
$username = $_POST['inputUsername'];
因为从表单中发布的数据在inputUsername下,而不在inputName下。 例如,如果您的表单中有一个字段,例如
<input type="text" class="form-control" name="example" value="" placeholder="Username" />
为了从该字段中的表单中获取数据,您将需要使用以下php代码获取此数据
$example = $_POST['example'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.