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 trialPierre Smith
11,842 PointsWhy can I send an email to my usrName@localhost but not to a real email.
within this lesson we have to send a email via swift mail and when I try to send one to my actual email it doesn't work however if I send it to localhost I get it in my terminal. Why is this? What am I doing wrong
Here is my index.php:
<?php
require 'vendor/autoload.php';
date_default_timezone_set('America/Toroto');
$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()); //gives us helpers like baseURL, currentURL, siteURL, urlFor
//this displays the root of the site (index page)
$app->get('/', function() use($app) {
$app->render('about.twig');
})->name('home');
//this displays the root of the site (index page)
$app->get('/contact', function() use($app) {
$app->render('contact.twig');
})->name('contact');
$app->post('/contact', function() use($app){
// var_dump($app->request->post());
$name = $app->request->post('name');
$email = $app->request->post('email');
$msg = $app->request->post('msg');
if(!empty($name) && !empty($email) && !empty($msg)) {
$sanitizedName = filter_var($name, FILTER_SANITIZE_STRING);
$sanitizedEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
$sanitizedMsg = filter_var($msg, FILTER_SANITIZE_STRING);
} else {
//message the user that there was a problem
$app->redirect('/contact');
}
// :: means static operator
$transport = Swift_MailTransport::newInstance();
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance();
$message->setSubject('Email From Test Site');
$message->setFrom(array(
$sanitizedEmail => $sanitizedName
));
$message->setTo(array('kipp0@localhost'));
$message->setBody($sanitizedMsg);
$result = $mailer->send($message); //sends mail and stores a boolean in result for if it failed
if($result < 1) { //message failed
//send user a message saying that the email failed
//log the failed attempt
//redirect to contact
$app->redirect('/contact');
} else {
//tell the user it worked and thank them
//redirect to contact
$app->redirect('/');
// var_dump($message);
}
});
$app->run(); //runs app (Obviously!)
?>
1 Answer
Ted Sumner
Courses Plus Student 17,967 PointsYou are not doing anything wrong. The local environment does not have the programming to actually send the email. It works on a real server, though.
Pierre Smith
11,842 PointsPierre Smith
11,842 PointsThank you.