我的index.html格式为:
<form method="post" action="mail.php">
<div class="col-sm-7">
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
</div>
<div class="col-sm-6 form group">
<input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
</div>
</div>
<textarea class="form-control" id="message" name="message" placeholder="Comment" rows="5"></textarea>
<div class="row">
<div class="col-sm-12 form-group">
<button class="btn btn-default pull-right" type="submit">Send</button>
</div>
</div>
</div>
</form>
和mail.php:
<?php
// Check for header injection
function has_header_injection($str){
return preg_match("/[\r\n]/", $str);
}
if(isset($_POST['submit'])){
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
// Check to see if $name or $email have header injections
if(has_header_injection($name) || has_header_injection ($email)){
die(); //if true kill the script
}
if(!$name || !$email || !$msg){
echo '<h2>All fields required</h2><a href="mail.php" class="button block">Go back and try again</a>';
exit;
}
// Add the recipient email to a variable
$to = "minusemp@gmail.com";
// Create a subject
$subject = "$name sent you a message via your website";
//Construct the message
$msg = "Name: $name\r\n";
$msg .= "Email: $email\r\n";
$msg .= "Message:\r\n$msg";
$msg = wordwrap($msg, 72);
// Set the mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email>\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email
mail($to, $subject, $msg, $headers)
}
?>
<script language="javascript"> window.location="index.html";</script>
我做错了什么?我在localhost和托管网站上测试了这个并且相同。当我提交表单完成它只是发送给我一个空白页面mail.php,没有错误,与PHP下面的脚本它发送回主页(这是我想要的)。我测试了添加:
if (mail($to, $subject, $body, $headers)===false) {
echo "Not sent!";
} else {
echo "Sent!";
}
但没有错误,它没有告诉我它是否被发送...所以我做错了什么?有人告诉我,我不能给自己发电子邮件,所以我让别人给我发电子邮件......没有运气,仍然没有发送电子邮件。
请帮助我坚持这个超过3天:\
答案 0 :(得分:0)
的index.html
<form method="post" action="mail.php">
<div class="col-sm-7">
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
</div>
<div class="col-sm-6 form group">
<input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
</div>
</div>
<textarea class="form-control" id="message" name="message" placeholder="Comment" rows="5"></textarea>
<div class="row">
<div class="col-sm-12 form-group">
<button class="btn btn-default pull-right" type="submit" name="submit">Send</button>
</div>
</div>
</div>
</form>
mail.php
<?php
// Check for header injection
function has_header_injection($str){
return preg_match("/[\r\n]/", $str);
}
if(isset($_POST['submit'])){
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
// Check to see if $name or $email have header injections
if(has_header_injection($name) || has_header_injection ($email)){
die(); //if true kill the script
}
if(!$name || !$email || !$msg){
echo '<h2>All fields required</h2><a href="mail.php" class="button block">Go back and try again</a>';
exit;
}
// Add the recipient email to a variable
$to = "minusemp@gmail.com";
// Create a subject
$subject = "$name sent you a message via your website";
//Construct the message
$msg = "Name: $name\r\n";
$msg .= "Email: $email\r\n";
$msg .= "Message:\r\n$msg";
$msg = wordwrap($msg, 72);
// Set the mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email>\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email
mail($to, $subject, $msg, $headers);
}
?>
<script language="javascript"> window.location="index.html";</script>
您在;
函数的末尾错过了mail()
。
始终$_POST['submit']
始终未定义,因此我们必须将name="submit"
添加到index.html中的按钮
答案 1 :(得分:0)
我不认为您的POST阵列包含“提交”字样。键。
if(isset($_POST['submit'])){
所以你的整个脚本都会跳过。