Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Class 'Swift_SendmailTransport' not found. What should i do?

When i'm submiting my form i get : Fatal error: Class 'Swift_SendmailTransport' not found in D:\xampp\htdocs\lr\index.php on line 45

$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('/lr/contact');
}

//Setup TRANSPORT for email
$transport = Swift_SendmailTransport::newInstance('C:/sendmail/ -bs');
//MAILER
$mailer = \Swift_Mailer::newIsntance($transport);
//MASSaGE OBJECT
$message = \Swift_Message::newInstance();
$message->setSubject('Email from our website');
$message->setFrom(array(
    $cleanEmail => $cleanName

));
//where to send message
$message -> setTo(array('treehouse@localhost'));
//body of message
$message -> setBody($cleanMsg);

//Sending Message
$result = $mailer->send($message);

if($result > 0){
    // send a message that says thank you!
    $app->redirect('/');

} else {
    //send message to the user that the message failed to send
    //log that there was an error
    $app->redirect('/lr/contact');
}

});

I'm working on Windows 7, my SENDMAIL located on C:/ Can you help me please. Working on localhost with XAMMP

1 Answer

Danil,

I don't see anywhere in your source code where you have included the file containing the Swift_SendmailTransport class. You can either download the source file for the class and include it in your code, or you can install the package via Composer and use an autoloader. The teacher's notes direct you to the SwiftMailer website: http://swiftmailer.org/. That should help get you started.