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 Sanitizing Form Data

Maxwell Kendall
seal-mask
.a{fill-rule:evenodd;}techdegree
Maxwell Kendall
Front End Web Development Techdegree Student 12,102 Points

Server error 500 occurring after creating transport variable and following

<?php

require 'vendor/autoload.php';
date_default_timezone_set('America/New_York');

$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 user that there was a problem
    $app->redirect('/contact');}
});

//SOMETHING IS WRONG WITH THIS CODE? Everything works fine when I comment the below code out//

$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'));
//sendmail must work inside server, otherwise you can input whatever^
$message->setBody($cleanMsg);

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

if($result > 0) {

  $app->redirect('/');
}

else {

  $app->redirect('/contact');
}
if($result > 0) {
    $app->redirect('/');
}

else {

  $app->redirect('/contact');
}

$app->run();

RETURNS-->

Server error

500 Details (The website encountered an error while retrieving http://port-80-tzgn9yrnis.treehouse-app.com/. It may be down for maintenance or configured incorrectly.)

I have loaded and reloaded all of the dependencies, slim, twig, swiftmailer, slim views etc... and still am seeing this error. I have also checked and rechecked my code in comparison with that of the tutorial and have found no discrepancies.

Please advise if possible as to why this error message is rendering.

Thank you!

1 Answer

Yes, you have the code incorrectly placed. Here is code that works properly. Note the location of the }); What you have written leaves that code dangling outside any routing. Also, look at how I edited your code quote so there is syntax highlighting for future reference.

<?php
$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 user 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('teds@biblewordstudy.net'));
    $message->setBody($cleanMsg);



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

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

$app->run();