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 trialYonas Fesehatsion
Full Stack JavaScript Techdegree Student 7,950 PointsError to send email from localhost
Slim Application Error The application could not run because of the following error:
Details
Type: Swift_TransportException Message: Expected response code 220 but got code "", with message "" File: /Applications/XAMPP/xamppfiles/htdocs/myfirstphp2/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php Line: 383
Yonas Fesehatsion
Full Stack JavaScript Techdegree Student 7,950 Points<?php
require 'vendor/autoload.php';
date_default_timezone_set('Africa/Addis_Ababa');
//$log = new Monolog\Logger('name');
//$log->pushHandler(new Monolog\Handler\StreamHandler('app.txt', Monolog\Logger::WARNING));
//$log->addWarning('Foo');
$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('yone920@gmail.com'));
$message->setBody($cleanMsg);
$result = $mailer->send($message);
if($result > 0) {
//Send a message that says thank you
$app->redirect('/');
} else {
//send a message to tthe user htat the message failed to send
//log that there was an error
$app->redirect('contact');
}
});
$app->run();
?>
8 Answers
miguelcastro2
Courses Plus Student 6,573 PointsGmail requires SSL. Make sure your transport is SSL enabled.
<?php
$transport->setEncryption('ssl');
?>
Jeremiah Wodke
3,192 PointsI'm getting the same error. However when I try to go to the file "AbstractSmtpTransport.php" within the lib/classes/swift/transport/ to fix the bug the file does not exist.
Craig Kuriger
37,104 PointsI'm also having the same problem as the original poster!
Ryan Mayo
23,604 PointsYou cannot send an email from this tutorial to your own email address in workspace..
$message->setTo(array('yone920@gmail.com')); will not work. rewatch video and he says that you have to send the email to the localhost i.e. $message->setTo(array( 'treehouse@localhost' )); then check it like he tells you using the console.
note, this is only in workspace, if you are doing this on your own server then i dont know..
Craig Kuriger
37,104 PointsI'm doing this on my own server, but thanks anyway!
cesarruelas
27,718 PointsHey Craig, could you make it work? I'm also doing it on my own server and it just didn't work no matter how many different things I tried
Mister Moody
48,333 PointsI have had this problem for the past two months~
Alena Holligan
Treehouse TeacherThis is what I did to make it work using gmail and XAMPP
Step 1 setup your transport for smtp
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465)
->setUsername(YOUR FULL EMAIL)
->setPassword(YOUR PASSWORD)
->setEncryption('ssl')
;
Step 2 also remember to change the "setTo" line to have your email
$message->setTo(array(YOUR FULL EMAIL));
Step 3 configure your gmail account
Allowing less secure apps to access your account https://support.google.com/accounts/answer/6010255
Thomas Pane
7,804 PointsI am having this same error message with no plausible explanation. I am connecting on my local host and i am using the correct code posted below:
$app->post('/press-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('/press-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) { // confirmation message that email sent } else { // say message failed to send }
});
Alena Holligan
Treehouse TeacherThomas Pane, are you using a local dev environment or workspaces?
Walter Allen
iOS Development with Swift Techdegree Student 16,023 PointsAlena, I am having the same issue as Thomas. I am working in Treehouse Workspaces. Here is the code as I have it, and I'm receiving the error: Expected response code 220 but got code "", with message ""
$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('/user/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 Thank You message to the user.
$app->redirect('/');
} else {
// Send a message to the user that message failed to send.
// Log that there was an error.
$app->redirect('/contact');
}
});
Any help you can give would be GREATLY appreciated. Thank you!
Walter Allen
iOS Development with Swift Techdegree Student 16,023 PointsAlena... never mind. I found my issue, finally. I had type 'user' instead of 'usr' in the parameter for Swift_SendmailTransport::newInstance. Duh.
Philip G
14,600 PointsPhilip G
14,600 PointsCould you please post the code you are using?