Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Stephanie Bercht
Stephanie Bercht
911 Points

PHP header not redirecting and not sending mail either... Help?

Hi,

Problem: The page doesn't redirect me anywhere, but it goes blank once I complete the form and submit. Mail does not get sent.

Context: Using PHPMailer and Gmail as SMTP.

I had this working before where it sent me to the right place and sent the mail. I had to move files around and must have lost something on the way... I can't find out what went wrong. Please help?

Here's the code it calls for form validation and email sending:

<?php

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $firstname = trim($_POST["firstname"]);
    $lastname = trim($_POST["lastname"]);
    $clinicname = trim($_POST["clinicname"]);
    $member = trim($_POST["member"]);
    $email =  trim($_POST["email"]);
    $message = trim($_POST["message"]);
    $subject = trim($_POST["subject"]);

//validating form
    if ($firstname == "" OR $lastname == "" OR $email == "" OR $message == "") {
        echo "You must provide a first name, an email address and message.";
        exit;
    }   

    foreach( $_POST as $value ){
        if( stripos($value, 'Content-Type:') !== FALSE ){
            echo "There was a problem with the information you entered.";
            exit;
        }
    }
//Spam bots filters
    if ( $_POST["address"] != "") {
        echo "your form submission has an error.";
    }


    require_once("inc/phpmailer/class.phpmailer.php"); 
    $mail = new PHPMailer(); 



    if (!$mail->ValidateAddress($email)){
        echo "Please use a valid email address.";
        exit;
    }

//message in email to be sent

    $email_body = "";
    $email_body = $email_body . "First name: " . $firstname . " " . $lastname . "<br>";
    $email_body = $email_body . "Clinic name: " . $clinicname . "<br>";
    $email_body = $email_body . "Email: " . $email . "<br>";
    $email_body = $email_body . "Member: " . $member . "<br>";
    $email_body = $email_body . "Subject: " . $subject . "<br>";
    $email_body = $email_body . "Message: " . $message;


//mail server set up

    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "smtp.gmail.com"; // sets the SMTP server
    $mail->Port       = 465;             // set the SMTP port for the GMAIL server
    $mail->SMTPSecure = "ssl";
    $mail->Username   = "my username";       // SMTP account username
    $mail->Password   = "my pw";        // SMTP account password

    $mail->SetFrom($email, $name);
    $mail->Subject    = "Hability Contact form | " . $lastname . ", Subject: " . $subject ;
    $mail->MsgHTML($email_body);

    $address = "steph@hability.net";
    $mail->AddAddress($address, "Contact Form");


    if(!$mail->Send()) {
      echo "There was a problem sending the email: " . $mail->ErrorInfo;
      exit;
    } 

header("Location: contact.php?status=thanks");
exit;

}



?>

And here's the contact form:

     <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>

            <div class="container">
                  <div style="text-align:center;" class="row">
                    <h2 class="featurette-heading" style="text-align:center; padding:0px"><b>
                      Thanks!</h2></b>
                      <p> We'll get in touch with you shortly.</p>
                  </div>
                  <div style="padding-top:30px" class="row">
                    <div class="col-sm-offset-4 col-sm-4 col-md-4 col-lg-4" style="text-align:center">
                      <p style="padding-top:20px;"><a href="http://www.hability.net/blog" class="blue-link"><i class="fa fa-pencil-square fa-lg"></i> Check out the Hability blog.</a></p> 
                      <p style="padding-top:20px;"><a href="https://twitter.com/intent/user?screen_name=HabilityPT" class="blue-link"><i class="fa fa-twitter-square fa-lg"></i> Follow us on Twitter @habilitypt</a></p>          
                    </div><!-- /.col-lg-4 -->
                  </div><!-- /.row -->
            </div><!--End Contact --> 



     <?php } else { ?>


            <div class="container">
                  <div style="text-align:center;" class="row">
                    <h2 class="featurette-heading" style="text-align:center; padding:0px"><b>Comment or question, we'd love to hear from you.</h2></b>
                  </div>


                  <div style="padding-top:30px" class="row">
                    <div class="col-sm-1"></div>
                    <div class="col-sm-4 col-md-4 col-lg-4">

                      <p style="padding-top:20px;">
                        <a href="mailto:info@hability.net" class="blue-link">
                        <i class="fa fa-envelope-square fa-lg"></i> <b>Email us</b> at info@hability.net</a>
                      </p>

                      <p style="padding-top:20px;"><a class="blue-link"><i class="fa fa-comments fa-lg"></i> <b>Text us</b> at 203.856.2419</a></p>

                      <p style="padding-top:20px;"><a class="blue-link"><i class="fa fa-phone-square fa-lg"></i> <b>Or call</b>, 203.856.2419</a></p>

                      <p style="padding-top:20px;"><a href="https://twitter.com/intent/user?screen_name=HabilityPT" class="blue-link"><i class="fa fa-twitter-square fa-lg"></i> Follow us on Twitter @habilitypt</a></p> 



                    </div><!-- /.col-lg-4 -->

                    <div class="col-sm-6 col-md-6 col-lg-6">


                            <form method="post" action="contactform.php" class="form-horizontal" id="contactForm">
                                  <legend><b>Send us a message:</b></legend>
                                    <div class="form-group">
                                      <label for="firstname" class="col-sm-3 control-label">Your Name</label>
                                      <div class="col-sm-4">
                                        <input type="text" name="firstname" class="form-control inputName" placeholder="First Name" >
                                      </div>
                                      <div class="col-sm-5">                 
                                        <input type="text" name="lastname" class="form-control inputName" placeholder="Last Name" >

                                      </div>
                                    </div>
                                     <div class="form-group">
                                      <label for="clinicname" class="col-sm-3 control-label inputName">Clinic</label>
                                      <div class="col-sm-9">
                                        <input type="text" class="form-control" name="clinicname" id="ClinicName" placeholder="Clinic Name">
                                      </div>
                                    </div>                       
                                    <div class="form-group">
                                      <label for="email" class="col-sm-3 control-label">Email Address</label>
                                      <div class="col-sm-9">
                                        <input type="email" name="email"  class="form-control" id="inputEmail" placeholder="Email">
                                      </div>
                                    </div>
                                    <div class="form-group">
                                      <label for="member" class="col-sm-3 control-label">Are you a Hability Member?</label>
                                      <div class="col-sm-9">
                                          <label class="radio-inline" id="member" name="member" >
                                          <input type="radio" name="member" id="memberYes" value="Yes"> Yes
                                        </label>
                                        <label class="radio-inline" name="member">
                                          <input type="radio" name="member" id="memberNo" value="No"> No
                                        </label>
                                      </div>
                                    </div>
                                    <div class="form-group">
                                      <label for="subject" class="col-sm-3 control-label">Subject</label>
                                          <div class="col-sm-9">
                                            <select class="form-control" name="subject">
                                              <option>Sales</option>
                                              <option>General Inquiry</option>
                                              <option>Support</option>
                                              <option>Other</option>
                                            </select> 
                                          </div> 
                                    </div>
                                    <div class="form-group">
                                      <label for="message" class="col-sm-3 control-label">Message</label>
                                      <div class="col-md-9">
                                        <textarea class="form-control" name="message" id="message" rows="3"></textarea>
                                      </div>  
                                    </div>  
                                    <!--SPAM bot filter-->
                                    <div class="form-group" style="display:none">
                                      <label for="address" class="col-sm-3 control-label">Address</label>
                                      <div class="col-sm-9">
                                        <p>Humans, please leave this blank!<p/>
                                        <input type="address" name="address"  class="form-control" id="inputEmail" placeholder="Address">
                                      </div>
                                    </div>
                                    <div class="form-group">
                                      <div class="col-sm-offset-9 col-sm-3" >
                                        <button type="submit" value="send" class="btn pull-right">Submit</button>
                                      </div>
                                    </div>
                            </form>
                    </div>



                  </div><!-- /.row -->
            </div><!--End Contact --> 

     <?php } ?>

1 Answer

Stephanie Bercht
Stephanie Bercht
911 Points

Problem solved. I had the wrong class.smpt.php in the wrong folder, I guess.