Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Building Websites with PHP Contact Form & Sending Email Sending Our Email

404 Page Not Found

When I press "Submit" i get to the "404 Page Not Found"-page. Here's my code:

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

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

$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->get('/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 { //mesage the user that 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'));
 $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();

Alexandru Palita
Alexandru Palita
14,258 Points

What worked for me locally on WAMP and having Test Mail Server Tool (local email catcher) installed :

<?php

require __DIR__ . '/vendor/autoload.php';

date_default_timezone_set('Europe/Paris');

$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');
    // var_dump($app->request->post('name'));
    // var_dump($name);

    if (!empty($name) && !empty($email) && !empty($msg)) {
        $cleanName = filter_var($name, FILTER_SANITIZE_STRING);
        $cleanEmail = filter_var($email, FILTER_SANITIZE_STRING);
        $cleanMsg= filter_var($msg, FILTER_SANITIZE_STRING);
    } else {
        //message the  user there was a problem
        echo "PROBLEM";
    }
//.........................................................THE FOLLOWING LINE MIGHT HELP YOU.....................................................

    $transport = Swift_MailTransport::newInstance('/usr/sbin/sendmail -bs'); 

// class Swift_SendmailTransport won't work

//.......................................................................END OF LINE................................................................................................
    $mailer = \Swift_Mailer::newInstance($transport);

    $message = \Swift_Message::newInstance();
    $message->setSubject('Email From Our Website');
    $message->setFrom(array(
        $cleanEmail => $cleanName
    ));
    $message->setTo(array('dev.ign.codes@gmail.com'));
    $message->setBody($cleanMsg);

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

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


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

$app->run();