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 Building Websites with PHP Contact Form & Sending Email Sending Our Email

Luke Douglas
Luke Douglas
13,256 Points

502 bad gateway error when sending message.

When I submit the forum the screen attempts to load for about 15 seconds then returns a screen with 502 bad gateway error. I have looked over my code for issues and was unable to find something that would cause this. I have posted the code below thank you for your help.

<?php 

require 'vendor/autoload.php';

$app = new \Slim\Slim( array(
  'view' => new \Slim\Views\Twig()
));


$view = $app->view();
$view->parserOptions = array(
    'debug' => true,
);

$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);

$app->get("/", function() use($app){
  $app->render('about.twig');
})->name('home');

$app->get("/contact", function() use($app){
  $app->render('contact.twig');
})->name('contact');

$app->post("/contact", function() use($app){
  $name = $app->request->post('name');
  $email = $app->request->post('email');
  $msg = $app->request->post('msg');

  if(!empty($name) && !empty($email) && !empty($msg)){
    $cleanName =filter_var($name, FILTER_SANITIZE_STRING);
    $cleanEmail =filter_var($email, FILTER_SANITIZE_EMAIL);
    $cleanMsg =filter_var($msg, FILTER_SANITIZE_STRING);
  } else{
    //message the usere there was a problem
    $app->redirect('/contact');
  }

  $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
  $mailer = \Swift_Mailer::newInstance($transport);

  $message = \Swift_Message::newInstance();
  $message->setSubject('Email From Our Website');
  $message->setFrom(array(
     $cleanEmail => $cleanName
  ));
  $message->setTo(array('treehouse@localhost'));
  $messge->setBody($cleanMsg);

  $result = $mailer->send($message);

  if($result > 0) {
    //send thank you messege
    $app->redirect('/');
  } else {
    //send message to user that failed to send
    //log that there was an error
    $app->redirect('/contact');
  }
});

Ok, I got this to work in a branch of my attempt of the same tutorial. The main prob I noticed is that you need to run the app (see bottom) also a misspelling where you set the: $messge->setBody($cleanMsg); instead of: $message->setBody($cleanMsg);

I also noted that the:
date_default_timezone_set('America/New_York'); was missing from the top of the page..

(you'll see where I used redirects to tell me where the "break" was. A good trick is to put redirects after large chunks of code and set the variable in question inside like so: $app->redirect($variable); this will break the site but return the variable as the site is seeing it.)

See included:

<?php 
date_default_timezone_set('America/New_York');    //*************missing************
require 'vendor/autoload.php';


$app = new \Slim\Slim( array(
  'view' => new \Slim\Views\Twig()
));


$view = $app->view();
$view->parserOptions = array(
    'debug' => true,
);

$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);

$app->get("/", function() use($app){
  $app->render('about.twig');
})->name('home');

$app->get("/contact", function() use($app){
  $app->render('contact.twig');
})->name('contact');

$app->post("/contact", function() use($app){
  $name = $app->request->post('name');
  $email = $app->request->post('email');
  $msg = $app->request->post('msg');

  if(!empty($name) && !empty($email) && !empty($msg)){
    $cleanName =filter_var($name, FILTER_SANITIZE_STRING);
    $cleanEmail =filter_var($email, FILTER_SANITIZE_EMAIL);
    $cleanMsg =filter_var($msg, FILTER_SANITIZE_STRING);

  } else{
    //message the usere there was a problem
    $app->redirect('/message filter problem');   // *******used redirect as a debugger**********
  }

  $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
  $mailer = \Swift_Mailer::newInstance($transport);

  $message = \Swift_Message::newInstance();
  $message->setSubject('Email From Our Website');
  $message->setFrom(array(
     $cleanEmail => $cleanName
  ));
  $message->setTo(array('treehouse@localhost'));
  $message->setBody($cleanMsg);                   // ************spelled message as mssge**********

  $result = $mailer->send($message);

  if($result > 0) {
    //send thank you messege
    $app->redirect('message_sent');   //*******used redirect as a debugger ******
  } else {
    //send message to user that failed to send
    //log that there was an error
    $app->redirect('message_not_sent');     // *******used redirect as a debugger*********
  }
});

$app->run();                                         //************** missing************
?>```

1 Answer

Luke Douglas
Luke Douglas
13,256 Points

Thank you for the help and the tips.