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 trialBryan Manhollan
Courses Plus Student 10,096 PointsI redirect properly, but I don't receive the mail.
I've checked my code against the code in the video. Pasting it below in case I missed something...
When I type "mail" into the console, it says "No mail for treehouse". The submission and the redirect all go through properly though. Not sure what's missing.
<?php require 'vendor/autoload.php'; date_default_timezone_set('America/New_York');
//$log = new Logger('name'); //$log->pushHandler(new StreamHandler('app.txt', 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->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'); }
$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();
3 Answers
Nathan Williams
Python Web Development Techdegree Student 6,851 PointsHey Bryan,
If you run postqueue -p
in the workspace terminal, do you see the messages listed there?
Regards,
Nathan
Bryan Manhollan
Courses Plus Student 10,096 PointsYes, I see 5 of them in there. I had tried a few times.
Nathan Williams
Python Web Development Techdegree Student 6,851 Pointsokay. it looks like something broke the email delivery to the user, but your code is correctly getting the email into the mail processing system (postfix). sorry for the confusion... i'm checking out what may have changed in our workspaces that would have broken it.
Bryan Manhollan
Courses Plus Student 10,096 PointsAwesome. Thanks Nathan.
Bryan Manhollan
Courses Plus Student 10,096 PointsBryan Manhollan
Courses Plus Student 10,096 PointsTo clarify, I redirect back to the home page, which should mean that a message is being sent.