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

Sending Our Email: Error sending the email

I'm following along on the PHP track, in the portion about creating a simple website. I'm at the point where I have tried to send the email out, but I get an exception. This one is called Swift_TransportException. The Message read: Expected response code 220 but got code "", with message "". I'll paste in the code here to show you want it looks like. All I added was the try and catch statements to catch the exception and echo out the message. // here is the code $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)) { // sanitize user-given information to remove unwanted characters $cleanName = filter_var($name, FILTER_SANITIZE_STRING); $cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL); $cleanMsg = filter_var($msg, FILTER_SANITIZE_STRING);

// use swiftmailer to send an email message
//First step, create a transporter and mailer object

$transporter = Swift_SendmailTransport::newInstance('usr/sbin/sendmail -bs');
$mailer = \Swift_Mailer::newInstance($transporter);

// next set up the information for the email (e.g. who it is from, who it is to, where it is going, and the message itself

$message = \Swift_Message::newInstance();
$message->setSubject('Email from our website');
$message->setFrom(array( $cleanEmail => $cleanName ));
$message->setTo(array( 'treehouse@localhost'));

// prepare to send message through the mail.  If the mail was sent, the return value will be greater than 0.  So first, check the result to see if the value is greater than 0.  That would mean that the email was sent properly

$result = $mailer->send($message);

if($result > 0) {
  // the resuklt was greater than zero, so redirect the user to the index page with a thank you message

} else { // Redirect user to the contact page with a warning about missing information in the contact form // insert warning code here about missing information in the contact page $app->redirect('/contact'); } } catch(Swift_TransportException $e) { // nothing going to happen echo $e->getMessage(); } } });

Please send help soon.
Sincerely your, lovable, unknowledgeable PHPer