[英]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.