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

Aaron HARPT
Aaron HARPT
19,845 Points

Contact Form Not Working on Live Site

I am currently having issues with my contact form not working on my live site. It works fine locally, but does not work on my server. I am using PHP with PHPmailer, then Postmark to take care of sending the emails, and a Web Hosting For Students custom email address. I am getting an error that says, in part, "Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting" Here is my code:

<?php
   if ($_SERVER["REQUEST_METHOD"] == "POST") {
     $name = trim(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING));
     $email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL));
     $message = trim(filter_input(INPUT_POST, "message", FILTER_SANITIZE_SPECIAL_CHARS));

     if ($name == "" || $email == "" || $message == "") {
       $error_message = "Please fill out all the required fields: Name, Email, and Message";
     }
     if (!isset($error_message) && $_POST["address"] != "") {
       $error_message = "Bad form input";
      }
      require("../includes/phpmailer/PHPMailer-master/class.phpmailer.php");
      require("../includes/phpmailer/PHPMailer-master/class.smtp.php");

      $mail = new PHPMailer();

      if (!isset($error_message) && !$mail->ValidateAddress($email)) {
        $error_message = "Invalid Email Address";
      }


      if (!isset($error_message)) {


      $email_body = "";
      $email_body .= $message . "\n" . " from " . $email;
      $mail->SMTPDebug = 3;
      $mail->isSMTP();                                      // Set mailer to use SMTP
      $mail->SMTPAuth = true;                               // Enable SMTP authentication
      $mail->Host = 'smtp.postmarkapp.com';  // Specify main and backup SMTP servers
      $mail->Port = 587;                                    // TCP port to connect to
      $mail->Username = 'Server API Token';                 // SMTP username
      $mail->Password = 'Server API Token';                           // SMTP password
      $mail->SMTPSecure = 'tls';                           // Enable TLS encryption, `ssl` also accepted

      $mail->setFrom(custom email address, $name);
      $mail->addAddress(gmail email address, 'Aaron Harpt');
      $mail->isHTML(false);
      $mail->Subject = 'Comment on Website from ' . $name;
      $mail->Body = $email_body;
      if ($mail->send()) {
        header("location:index.php?status=thanks");
        exit;
      }
        $error_message = 'Message could not be sent.';
        $error_message .= 'Mailer Error: ' . $mail->ErrorInfo;
    }
   }