表单显示成功消息但未收到任何电子邮件

时间:2014-03-09 07:46:23

标签: php forms email twitter-bootstrap post

我有一个Bootstrap 3表单,显示成功消息:“您的消息已发送”但我没有收到任何电子邮件。甚至不在我的垃圾邮件/垃圾邮件文件夹中。它验证正确,但没有发送电子邮件。这是PHP代码。请帮忙!

PHP

    <?php
// check if fields passed are empty
if(empty($_POST['firstname'])   ||
     empty($_POST['lastname'])      ||
     empty($_POST['address'])       ||
     empty($_POST['address2'])      ||
     empty($_POST['city'])          ||
     empty($_POST['state_province'])        ||
     empty($_POST['country'])       ||
     empty($_POST['zip'])           ||
     empty($_POST['phone'])           ||
   empty($_POST['email'])         ||
   empty($_POST['checkbox'])        ||
   empty($_POST['comment'])   ||
   {
    echo "No arguments Provided!";
    return false;
   }

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state_province = $_POST['state_province'];
$country = $_POST['country'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];
$checkbox = $_POST['checkbox'];
$message = $_POST['comment'];

// create email body and send it    
$to = 'email@example.com'; // put your email
$email_subject = "Contact form submitted by:  $firstname";
$email_body = "You have received a new message. \n\n".
                  "Here are the details:\n \nFirst Name: $firstname \n Last Name: $lastname\n Address: $address\n Address2: $address2\n City: $city\n State/Province: $state_province\n Country: $country\n zip: $zip\n Phone: $phone\n Email: $email_address\n Checkbox: $checkbox\n Message: \n $message";
$headers = "From: $email_address\n";
$headers = "Reply-To: $email_address";  
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

HTML

<form class="form-horizontal" role="form" id="ContactUs" novalidate>
                    <!-- First Name -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="firstname"><abbr title="Required field">*</abbr>First Name</label>
                      <div class="col-md-8">
                        <input type="text" class="form-control" id="firstname" name="firstname" placeholder="First Name" required data-validation-required-message="Please enter your first name">
                        <div class="help-block"></div>
                      </div>
                    </div>
                    <!-- Last Name -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="lastname"><abbr title="Required field">*</abbr>Last Name</label>
                      <div class="col-md-8">
                        <input type="text" class="form-control" id="lastname" name="lastname" placeholder="Last Name" required data-validation-required-message="Please enter your last name">
                        <div class="help-block"></div>
                      </div>
                    </div>
                    <!-- Address -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="address"><abbr title="Required field">*</abbr>Address</label>
                      <div class="col-md-8">
                        <input type="text" class="form-control" id="address" name="address" placeholder="Address" required data-validation-required-message="Please enter your address">
                        <div class="help-block"></div>
                      </div>
                    </div>
                    <!-- Address 2 -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="address2">Address 2</label>
                      <div class="col-md-8">
                        <input type="text" class="form-control" id="address2" name="address2" placeholder="Address 2">
                      </div>
                    </div>
                    <!-- City -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="city"><abbr title="Required field">*</abbr>City</label>
                      <div class="col-md-8">
                        <input type="text" class="form-control" id="city" name="city" placeholder="City" required data-validation-required-message="Please enter your city">
                        <div class="help-block"></div>
                      </div>
                    </div>
                    <!-- State/Province -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="state_province"><abbr title="Required field">*</abbr>State/Province</label>
                      <div class="col-md-8">
                        <input type="text" class="form-control" id="state_province" name="state_province" placeholder="State/Province" required data-validation-required-message="Please enter your State or Province">
                        <div class="help-block"></div>
                      </div>
                    </div>
                    <!-- Country -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="country"><abbr title="Required field">*</abbr>Country</label>
                      <div class="col-md-8">
                        <input type="text" class="form-control" id="country" name="country" placeholder="Country" required data-validation-required-message="Please enter your Country of residence">
                        <div class="help-block"></div>
                      </div>
                    </div>
                    <!-- Zip/Postal Code  -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="zip"><abbr title="Required field">*</abbr>Zip/Postal Code</label>
                      <div class="col-md-8">
                        <input type="text" class="form-control" id="zip" name="zip" placeholder="Zip/Postal Code" required data-validation-required-message="Please enter your Zip or Postal Code">
                        <div class="help-block"></div>
                      </div>
                    </div>
                    <!-- Phone -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="phone">Phone</label>
                      <div class="col-md-8">
                        <input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone">
                      </div>
                    </div>
                    <!-- Email Address -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="email"><abbr title="Required field">*</abbr>Email address</label>
                      <div class="col-md-8">
                        <input type="email" class="form-control" id="email" name="email" placeholder="Email Address" required data-validation-required-message="Please enter your email">
                        <div class="help-block"></div>
                      </div>
                    </div>
                    <!-- Mailing List -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="checkbox">Include in mailing list</label>
                      <div class="col-md-8">
                        <input type="checkbox" class="form-control" id="checkbox" name="checkbox" value="1">
                      </div>
                    </div>
                    <!-- Comment -->
                    <div class="form-group">
                      <label class="control-label col-md-4" for="comment">Comment</label>
                      <div class="col-md-8">
                        <textarea class="form-control" id="comment" name="comment" rows="3"></textarea>
                      </div>
                    </div>
                    <!-- Buttons -->
                    <div class="form-group">
                       <div class="col-md-8 col-md-offset-4">
                            <div id="success"> </div> <!-- For success/fail messages -->
                          <button type="submit" class="btn btn-primary pull-right">Submit</button>
                       </div>
                    </div>
                </form>

更新时间下午8:50 03/09

我将此内容上传到其他主机,现在,当我提交表单时出现错误:“无法发送消息!”

1 个答案:

答案 0 :(得分:0)

您要将电子邮件发送至&#34; email@example.com"

通过将$更改为变量,可能会尝试将其发送到您的电子邮件地址。

同时更改标题。来自这个

$headers = "From: $email_address\n";
$headers = "Reply-To: $email_address";  

要:

$headers = "From: $email_address\n";
$headers .= "Reply-To: $email_address";  

注意第二行上的。=,这意味着将字符串添加到标题上并替换变量的内容。

也许可以改变这个:

mail($to,$email_subject,$email_body,$headers); 
return true;

return mail($to,$email_subject,$email_body,$headers); // will return true on success send, or false if it failed to send.