我创建了一个使用bootstrap设计的表单,并按照PHP教程介绍了如何使表单正常运行(当信息被填入电子邮件以发送到指定的电子邮件时)但它不起作用。
我在Stackoverflow上读过很多线程但找不到解决方案。
我配置了我的php.ini文件(我认为正确),而且mail()函数仍然不起作用。
以下是我的代码。我是PHP的绝对初学者,我真的很感谢你的帮助!
<?php
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])){
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
if (has_header_injection($name)||($email)) {
die;
}
if ( !$name || !$email || !$message ) {
echo '<h2 class="success">All fields are required</h2> <a href="contact.php" class="btn btn-custom btn-sm">Try again</a>';
}
$to = "example@email.com"; (this has my true email)
$subject = "$name sent you a message via example's contact form";
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message:\r\n$msg";
$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";
mail($to, $subject, $message, $headers);
?>
<h2 class="success"> Thank you! Your project request is submitted. We will get back to you within 24 hours.</h2>
<p><a href="index.php" class="btn btn-custom btn-sm">« Go to home page</a></p>
<?php } else { ?>
<form action="" class="contact-form" id="contact-form" method="post" role="form">
<div class="form-group">
<label class="sr-only" for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" required="" placeholder="Name">
</div>
<div class="form-group">
<label class="sr-only" for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" required="" placeholder="Email">
</div>
<div class="form-group">
<textarea class="form-control" id="message" name="message" placeholder="Enter a brief description of your project" required=""></textarea>
</div>
<button type="submit" class="btn btn-custom" name="contact_submit">Send</button>
</form>
<?php } ?>
答案 0 :(得分:2)
您是否正在使用localhost,如果是,则电子邮件功能无效,请将您的代码放在服务器上。