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 trialSean Flanagan
33,235 PointsCan't send message
Hi. When I try to send a message, all I get is:
Fatal error: Class 'Swift_SendMailTransport' not found in /home/treehouse/workspace/index.php on line 44
Below is my index.php file:
<?php
require 'vendor/autoload.php';
date_default_timezone_set("Europe/London");
//$log = new Logger('name');
//$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));
//$log->addWarning('Oh Noes.');
// Instantiate new Slim application
$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 {
//tell the user there was a problem
$app->redirect("/contact");
}
//error reported on next line
$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 thank-you message
$app->redirect("/");
} else {
//send message to user that message failed to send
//log an error
$app->redirect("/contact");
}
});
// Run Slim application
$app->run();
I'll paste a link to a snapshot of my work space if it makes things easier:
Thanks in advance.
3 Answers
Alena Holligan
Treehouse TeacherSean Flanagan, looks like a couple little errors
1) wrong case for Swift_SendmailTransport, use
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
2) setFrom needs to have => instead of ->. So use
$message->setFrom(array(
$cleanEmail => $cleanName
));
Sergey Podgornyy
20,660 PointsDid you set up your .htaccess file on proper way?
Here is my project on Github. You can compare your code with mine.
Sean Flanagan
33,235 PointsHi Sergey. Thanks for your reply. I hope you and your family have had a happy Christmas.
As far as I know, I've set up .htaccess as instructed. Unfortunately, there's no preview of it available so I can't be 100% sure. I remember that Hampton wanted it that way but I found out through LinkedIn that he doesn't work for Treehouse any more.
And I've never used Github. Would it be best to register there?
Thanks Sergey.
Sergey Podgornyy
20,660 PointsThank you, Sean. Hope you also had happy Christmas =)
You can upload your projects on Github. If you want just download someone project or just look on it, registration is not important. I recommend you to learn this course about Git Basics. Hope you'll find out this course useful for you.
About your code, I'll try it at home on my local machine. I believe we will find a solution to this problem together ;) Just for now, here is link where you can find my .htaccess file - https://github.com/SergeyPodgornyy/Treehouse-PHP_Website/blob/master/.htaccess.
Happy New Year!
Sean Flanagan
33,235 PointsThank you Sergey.
I noticed that Line 2 in my index.php file reads:
require 'vendor/autoload.php';
whereas you have:
require __DIR__ . '/vendor/autoload.php';
This takes you to the root directory, doesn't it? I wonder if this is why I can't send an email.
Happy New Year to you also and thank you for your help. :)
Sergey Podgornyy
20,660 PointsMagic constant DIR return the directory of the file. More about magic constants you can read on php.net manual
Sean Flanagan
33,235 PointsSean Flanagan
33,235 PointsHi Alena. Thanks for your help.
I've changed the upper-case S to a lower-case and the dash after $cleanEmail to an equals sign as you suggested. I then previewed the program and when I tried to send an email I got another error message. I realised that I'd left out the dollar sign in
if ($result > 0)
and rectified that.I ran another preview and sent myself another message. Clicking Submit took me to the About page. I watched the video and used the console to check and delete my mail there.
Nothing like a little teamwork! Thank you both and happy New Year!