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

James Barrett
James Barrett
13,253 Points

Sending mail with PHPMailer and Postmark error

I am thrown a SMTP connect() failed error upon submitting my contact form.

After using SMTPDebug = 2 I am shown the following error: SMTP ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

I am using a local environment to test my website; MAMP.

I have no idea what to do! My email signature is setup on Postmark and the SMTP server is enabled. Here is my code:

<?php

ini_set('display_errors', '1');

if($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = trim(filter_input(INPUT_POST, "user_name", FILTER_SANITIZE_STRING));
  $email = trim(filter_input(INPUT_POST, "user_email", FILTER_SANITIZE_EMAIL));
  $human = trim(filter_input(INPUT_POST, "user_human", FILTER_SANITIZE_STRING));
  $message = trim(filter_input(INPUT_POST, "user_message", FILTER_SANITIZE_STRING));

  // No input validation
  if($name == "" || $email == "" || $human == "" || $message == "") {
    echo "Please fill in the required fields: Name, Email, Are you human, Message";
    exit;
  }

 // Prevent HoneyPot Attacks
  if($_POST["check"] != "") {
    $error_message = "Form Input Incorrect";
    exit;
  }

  require("inc/PHPMailer/PHPMailerAutoload.php");

  $mail = new PHPMailer;

  // Invalid Email Validation
  if(!$mail->ValidateAddress($email)) {
    echo "Invalid Email Address";
    exit;
  }

  $email_body = "";
  $email_body .= "Name: " . $name ."\n";
  $email_body .= "Email: " . $email ."\n";
  $email_body .= "Message: " . $message ."\n";

  $mail->IsSMTP();
  $mail->SMTPAuth = true;
  $mail->SMTPDebug = 2;
  $mail->Host = "smtp.postmarkapp.com";
  $mail->Port = 26;
  $mail->Username = "#";
  $mail->Password = "#";

  $mail->setFrom($email, $name);
  $mail->addAddress("UP734253@myport.ac.uk", "James");
  $mail->isHTML(false);
  $mail->Subject = 'Message from ' . $name;
  $mail->Body    = $email_body;

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

  header("location:contactpage.php?status=thanks");
}

Any ideas where to start?

2 Answers

Check to see if your PHP is using the openSSL extension and enable it if it is not

Also I'm not sure if this is the issue, but http://developer.postmarkapp.com/developer-send-smtp.html lists three ports in the Connection details section. One of these may work for you:

Ports - 25, 2525, or 587

James Barrett
James Barrett
13,253 Points

The port was the problem! Now I am receiving email. I am now being faced with another minor error. I receive the email on PostMark however I am prompted with this error:

https://i.gyazo.com/2c3626865bf093c901b74a5222ada819.png

It's not affecting the contents of the email. I do not know what exactly is being affected...

Any help would be fantastic.

Thanks Lindsay for your contribution!

Hey James,

It sounds like your signature has been created on Postmark, but it might be worth double-checking your sender signatures just to confirm that the From address is the same as the one used to create the sender signature, and that you've activated your signature by confirming you have access to the From email set in the signature.

Adding DKIM and SPF may help too, to increase deliverability by proving email authenticity.

Hopefully one of these solves the error!