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

Ciaran McNally
Ciaran McNally
7,052 Points

Blank page after submitting form

When i submit the form I am redirected to a blank page and the mail is not sent. Code below

require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('Europe/Belfast');

//$log = new Monolog\Logger('name');
//$log->pushHandler(new Monolog\Handler\StreamHandler('app.txt', Monolog\Logger::WARNING));
//$log->addWarning('Foo');



$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 {
   $app->redirect('/contact'); 
  }

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

  $message = \Swift_Mailer::newInstance();
  $message->setSubject('Email from our website');
  $message->setFrom(array($cleanEmail => $cleanName));

  $message->setTo(array('treehouse@localhost'));
  $message->setBody($cleanMsg);

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

  if($result > 0){
  //send a message to thank
  $app->redirect('/');  
  } else {
  //message failed
  //log error
  $app->redirect('/contact');
  }


});

$app->run();



?>

Code for form:
    <form action="" method="post"> 
      <fieldset>
        <input name="name" type="text" placeholder="Full Name">
        <input name="email" type="email" placeholder="Email Address">
        <textarea name="msg" placeholder="Your message..."></textarea>
      </fieldset>
      <input type="submit" class="button">
    </form>```
Darren Hill
Darren Hill
Courses Plus Student 9,340 Points

btw you can format you code by placing "```" (without the quotes) before and after the code you want formatted

:) read the "Markdown Cheatsheet" link below

2 Answers

Darren Hill
PLUS
Darren Hill
Courses Plus Student 9,340 Points

There's no function in the

<form action="" .....

so it doesn't know where to send the information to

or i might be wrong

D

Ciaran McNally
Ciaran McNally
7,052 Points

I think the code is correct, I went through the videos twice now. Checked the console in chrome and it is saying 500 internal server error. Could this be an issue on treehouses side?

Jackie Keiser
Jackie Keiser
9,833 Points
$message = \Swift_Mailer::newInstance();

should be

$message = \Swift_Message::newInstance();

I would also suggest going through each line after $app->post and adding a redirect and then trying to submit. If it redirects, everything up until that line works. I kept doing this until I found a line where it wouldn't redirect and then I knew it was the previous statement that was giving me problems.