我已经下载了一个 Bootstrap 网站模板,并试图使发送到我的电子邮件地址的表单正常工作。当所有评论都已填写时,它给出了所有内容都已提交的消息,但没有发送任何实际消息。
这是索引中的 HTML block :
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>Contact Us</h2>
<hr class="star-primary">
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<!-- To configure the contact form email address, go to mail/contact_me.php and update the email address in the PHP file on line 19. -->
<!-- The form should work on most web servers, but if the form is not working you may need to configure your web server differently. -->
<form name="sentMessage" id="contactForm" novalidate>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name</label>
<input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email Address</label>
<input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Phone Number</label>
<input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message</label>
<textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-success btn-lg" action="public_html/mail/contact_me.php">Send</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
这是 PHP 文件中的代码:
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'tri.developmentstudios@gmail.com'; // Add your email address inbetween the '' replacing
yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the
details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@tri-dev.com\n"; // This is the email address the generated message will
be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
请帮忙!
提前致谢
最佳答案
您的表单没有方法,所以很可能它正在执行 $_GET
而不是 $_POST
:
<form name="sentMessage" id="contactForm" novalidate method="post" action="public_html/mail/contact_me.php">
您的输入中没有名称 (name=""
):
<input name="email" type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
此外,您可以更好地分解脚本:
function Validate()
{
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
return false;
}
else
return true;
}
function SendEmail($to = 'tri.developmentstudios@gmail.com')
{
if(Validate() == true) {
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the
details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@tri-dev.com\n";
$headers .= "Reply-To: $email_address";
// Send true on successful send.
// Send false if failed
return (mail($to,$email_subject,$email_body,$headers))? true: false;
}
else
// Invalid inputs
return 'err';
}
// Apply function(s). You will get true, false, or err
$send = SendEmail();
// On return, you can echo any result
if($send == 'err')
echo 'Invalid Fields.';
elseif($send == false)
echo 'An Error Occurred.';
else
echo 'Email Sent Successfully.';
关于php - HTML 电子邮件表单不发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27455063/