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

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.