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 trial

PHP Building Websites with PHP Contact Form & Sending Email Sending Our Email

Yonas Fesehatsion
seal-mask
.a{fill-rule:evenodd;}techdegree
Yonas Fesehatsion
Full Stack JavaScript Techdegree Student 7,950 Points

Error 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

Could you please post the code you are using?

Yonas Fesehatsion
seal-mask
.a{fill-rule:evenodd;}techdegree
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

Gmail requires SSL. Make sure your transport is SSL enabled.

<?php

$transport->setEncryption('ssl');
?>
Jeremiah Wodke
Jeremiah Wodke
3,192 Points

I'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.

I'm also having the same problem as the original poster!

You 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..

I'm doing this on my own server, but thanks anyway!

cesarruelas
cesarruelas
27,718 Points

Hey 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

I have had this problem for the past two months~

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

This 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
Thomas Pane
7,804 Points

I 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
Alena Holligan
Treehouse Teacher

Thomas Pane, are you using a local dev environment or workspaces?

Walter Allen
seal-mask
.a{fill-rule:evenodd;}techdegree
Walter Allen
iOS Development with Swift Techdegree Student 16,023 Points

Alena, 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Walter Allen
iOS Development with Swift Techdegree Student 16,023 Points

Alena... never mind. I found my issue, finally. I had type 'user' instead of 'usr' in the parameter for Swift_SendmailTransport::newInstance. Duh.