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 Build a Basic PHP Website (2018) Adding a Basic Form Using A Third-Party Library

PHPMailer: 500 Internal Server Error

I've been attempting to set up PHPMailer on a website and I keep getting a 500 Internal Server Error. I have no idea what the problem is. Can someone please help me out? I would greatly appreciate it.

<?php

// BioAssay Sciences PHPMailer

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
    $email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_STRING));
    $phone = trim(filter_input(INPUT_POST,"phone",FILTER_SANITIZE_STRING));
    $location = trim(filter_input(INPUT_POST,"location",FILTER_SANITIZE_STRING));
    $company = trim(filter_input(INPUT_POST,"company",FILTER_SANITIZE_STRING));
    $message = trim(filter_input(INPUT_POST,"message",FILTER_SANITIZE_STRING));
    $email_body = "Potential Client from the BioAssay Sciences Contact Form:\n\n";

    if ($name == ""){
        echo "Please include your name.";
        exit;
    } elseif ($email == ""){
        echo "Please include your email address.";
        exit;
    } elseif ($phone == ""){
        echo "Please include your phone number.";
        exit;
    } elseif ($message == ""){
        echo "Please include your message.";
        exit;
    } else {
        continue;
    }


    if ($_POST["address"] != "") {
      echo "Your form submission has an error.";
      exit;
    }


    include($_SERVER["DOCUMENT_ROOT"] . "/inc/phpmailer/class.phpmailer.php");

    $mail = new PHPMailer; 

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

    $email_body .= "Name: " . $name . "\n";
    $email_body .= "Email Address: " . $email . "\n";
    $email_body .= "Phone Number: " . $phone . "\n";
    $email_body .= "Company: ". $company . "\n";
    $email_body .= "Location: " . $location . "\n";
    $email_body .= "Message: " . $message . "\n";

    $mail->setFrom($email, $name);
    $mail->addAddress('drlittle@dr-tom.com', 'Thomas A. Little');     // Add a recipient
    $mail->addCC('webmaster@bioassaysciences.com');

    $mail->isHTML(false);                                  // Set email format to HTML

    $mail->Subject = 'BioAssay Sciences Contact from ' . $name;
    $mail->Body    = $email_body;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
        exit;
    } 


    header("Location: /thankyou_url.php");
}
?>

<?php 

    $page_title = "BioAssay Sciences - Building a Better Assay";
    $page_description = "";

    // Be sure to set the current page to 'active' for navigation highlighting
    $active_about = "";
    $active_platform = "";
    $active_cost = "";
    $active_productivity = "";
    $active_clients = "";
    $active_contact = "active";

    include($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php");


?>


        <div class="breadcrumb-wrap">
            <div class="container">
                <div class="row">
                    <div class="col-sm-6">
                        <h4>Contact Information</h4>
                    </div>
                    <div class="col-sm-6 hidden-xs text-right">
                        <ol class="breadcrumb">
                            <li><a href="/index.php">Home</a></li>
                            <li>Contact</li>
                        </ol>
                    </div>
                </div>
            </div>
        </div><!--breadcrumbs-->
        <div class="divide80"></div>
        <div class="container">
            <div class="row">
                <div class="col-md-8 margin30">
                    <h3 class="heading">A Global Company</h3>
                    <p>
                        Please use the following form to contactact BioAssay Sciences. Please provide us with as much detail as possible, including your company, location, requirements, and project information. We will reply to you within 24 hours.
                    </p>
                    <div class="divide30"></div>
                    <div class="form-contact">
                        <div class="required">
                            <p>( <span>*</span> fields are required )</p>
                        </div>

                       <form action="/contact/index.php" method="post">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="row control-group">
                                        <div class="form-group col-xs-12 controls">
                                            <label for="name">Name<span>*</span></label>
                                            <input type="text" class="form-control" placeholder="Name" id="name" name="name" required>
                                            <p class="help-block"></p>
                                        </div>
                                    </div>

                                </div>

                                <div class="col-md-6">
                                    <div class="row control-group">
                                        <div class="form-group col-xs-12 controls">
                                            <label for="email">Email Address<span>*</span></label>
                                            <input type="email" class="form-control" placeholder="Email Address" id="email" name="email" required>
                                            <p class="help-block"></p>
                                        </div>
                                    </div> 
                                </div>
                            </div>
                            <div class="row control-group">
                                <div class="form-group col-xs-12  controls">
                                    <label for="phone">Phone Number<span>*</span></label>
                                    <input type="tel" class="form-control" placeholder="Phone Number" id="phone" name="phone" required>
                                    <p class="help-block"></p>
                                </div>
                            </div>
                            <div style="display: none">
                                <label for="address">Address:</label><br>
                                <input type="text" name="address" id="address" placeholder="1234 Henderson Way" name="address"><br><br>
                                <p>Please leave the address field blank.</p>
                                </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="row control-group">
                                        <div class="form-group col-xs-12 controls">
                                            <label for="company">Company</label>
                                            <input type="text" class="form-control" placeholder="Company" id="company" name="company">
                                            <p class="help-block"></p>
                                        </div>
                                    </div>

                                </div>

                                <div class="col-md-6">
                                    <div class="row control-group">
                                        <div class="form-group col-xs-12 controls">
                                            <label for="location">Location</label>
                                            <input type="text" class="form-control" placeholder="Location" id="location" name="location">
                                            <p class="help-block"></p>
                                        </div>
                                    </div> 
                                </div>
                            </div>
                            <div class="row control-group">
                                <div class="form-group col-xs-12 controls">
                                    <label for="message">Message<span>*</span></label>
                                    <textarea rows="5" class="form-control" placeholder="Message" id="message" name="message" required></textarea>
                                    <p class="help-block"></p>
                                </div>
                            </div>
                            <br>
                            <div id="success"></div>
                            <div class="row">
                                <div class="form-group col-xs-12">
                                    <button type="submit" class="btn btn-theme-bg btn-lg">Send Message</button>
                                </div>
                            </div>
                        </form>

                    </div><!--contact form-->
                </div>
                <div class="col-md-4">
                    <h3 class="heading">Contact info</h3>
                    <ul class="list-unstyled contact contact-info">
                        <li><p><strong><i class="fa fa-map-marker"></i> Address:</strong> <br> Thomas A. Little <br> 12401 N Wildflower Lane <br> Highland, UT 84003</p></li> 
                        <li><p><strong><i class="fa fa-envelope"></i> Email:</strong> <a href="mailto: drlittle@dr-tom.com">drlittle@dr-tom.com</a></p></li>
                        <li> <p><strong><i class="fa fa-phone"></i> Phone:</strong> +1 925 285-1847</p></li>
                    </ul>
                    <div class="divide40"></div>
                    <h4>Social Media</h4>
                     <div class=" margin10">
                            <a href="https://www.linkedin.com/in/thomas-little-516766b" class="social-icon si-dark si-colored-linkedin" target="_blank">
                                <i class="fa fa-linkedin"></i>
                                <i class="fa fa-linkedin"></i>
                            </a>
                            <a href="https://twitter.com/TLC_Consulting" class="social-icon si-dark si-colored-twitter" target="_blank">
                                <i class="fa fa-twitter"></i>
                                <i class="fa fa-twitter"></i>
                            </a>
                            <a href="https://www.facebook.com/thomas.little.58367" class="social-icon si-dark si-colored-facebook" target="_blank">
                                <i class="fa fa-facebook"></i>
                                <i class="fa fa-facebook"></i>
                            </a>
                            <a href="https://www.youtube.com/channel/UCDphG55AIuqg_wGk7YGKNMQ" class="social-icon si-dark si-colored-youtube" target="_blank">
                                <i class="fa fa-youtube"></i>
                                <i class="fa fa-youtube"></i>
                            </a>
                        </div><!--socials-->
                </div>
            </div>
        </div>
        <div class="divide40"></div>


<?php

    include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php");

?>

I found the solution, if anyone is interested.

 if ($name == ""){
        echo "Please include your name.";
        exit;
    } elseif ($email == ""){
        echo "Please include your email address.";
        exit;
    } elseif ($phone == ""){
        echo "Please include your phone number.";
        exit;
    } elseif ($message == ""){
        echo "Please include your message.";
        exit;
    } else {
        continue;
    }

The problem is where I told the program to "continue". I removed that and it worked perfectly.