【发布时间】:2016-06-24 01:27:47
【问题描述】:
我已经尝试了几个小时来解决这个问题,但无法弄清楚我做错了什么。
我创建了一个 Wordpress 联系页面,但是当我尝试将测试电子邮件发送给自己时,却没有任何结果。我没有收到任何错误或任何东西——我在这里错过了什么?感谢您的关注,如果您需要任何额外信息,请告诉我。
HTML/PHP:
<!--------------- YOUR INFO -------------------->
<div class="col-xs-10 col-xs-offset-1 col-lg-6 col-lg-offset-3" id="contactWrapper">
<h3 id="contactSectionHeader" class="p-t-2"><i class="fa fa-plus-square-o"></i> YOUR INFORMATION</h3>
<form class="form-horizontal" role="form" method="post" action="contactform.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
PHP/发送页面:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'brandon@brandonstiles.com';
$subject = 'Message from Contact Demo ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (wp_mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
【问题讨论】:
-
我不认为这会给您带来错误,但您应该注意
wp_mail()实际上并没有采用$from参数。它取而代之的是一个数组或标题字符串。但是你传入一个字符串,所以我认为它不会导致失败,但你应该熟悉文档。请参阅此链接:developer.wordpress.org/reference/functions/wp_mail。
标签: php wordpress contact-form