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 trialTal Goren
2,601 Pointspage won't redirect and won't send mail
I looked again and agan and i cant find what is wrong
<?php
require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('Europe/Paris');
/*
$log = new Monolog\Logger('name');
$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));
$log->addWarning('oh no');
*/
$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_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) {
$app->redirect('/');
} else {
$app->redirect('/contact');
}
});
$app->run();
?>
2 Answers
Ted Sumner
Courses Plus Student 17,967 PointsThere are a bunch of threads regarding this problem. One recent one I participated in regarding redirection. The problem might be in your twig files, particularly the nav links.
The email will not send because you are not on a live server (most likely). I did not do this course in Workspaces, so I don't know the process for checking email on it. I had no email until I went live at http://vetsrights.org.
Read the other posts and see what you figure out. If you have no luck with that, post a snapshot of your workspace here by clicking on the camera icon at the upper right and follow the steps until you can copy the url to paste here.
William McManus
14,819 PointsHey, Im running this locally on my Mac in Terminal and php -S localhost:8000. It all works great but no email gets sent to terminal/mail, or by actual email I entered. Is this just because of the same reason? that it is not actually live, just "sort of live" virtually on my machine?
Tal Goren
2,601 Pointsthanks for your response.
well.. .at the end it was a spacing mistake on the contact.twig file, my "if" statement didn't pass through
<form action="" method="post"> <fieldset> <input name="name" type="text" placeholder="Full Name"> <input name="email "type="email" placeholder="Email Address"> //the quotation_mark was stuck to type="email" <textarea name="msg" placeholder="Your message..."></textarea> </fieldset> <input type="submit" class="button"> </form>
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 Pointsedited to format code