简体   繁体   中英

Form shows success message but no email is received

I have a Bootstrap 3 form that displays the success message: "Your message has been sent" but I don't receive any emails. Not even in my spam/junk folder. It validates properly but no email is sent. Here's the PHP code. Please help!

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>

UPDATE 8:50PM 03/09

I uploaded this to a different host and now, when I submit the form I'm getting an error : "Message cannot be sent!"

you are sending the email to "email@example.com"

Probably try sending it to your email address, by changing the $to variable.

Also change the headers. From This

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

To:

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

note the .= on the second line, this means add the string onto headers vs replacing the content of the variable.

And maybe change this:

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM