1

I am new to Swiftmailer and am trying to send mail via a HTML contact form on a localhost (XAMPP). After pressing the 'submit' button, the error reads:

Fatal error: Class 'Swift_SmtpTransport' not found in /Applications/XAMPP/xamppfiles/htdocs/testsite/sendmessage.php on line 23

Line 23, 24, and 25 is this section:

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
    ->setUsername('foobar@googlemail.com')
    ->setPassword('GENERATED PASSWORD');

I am expecting the mail to send at this point, and show me a 'Mail Sent' message, along with the echoed lines within the PHP file, however while I'm seeing a "Mail Sent" confirmation I am also getting the error, and I do not receive an email.

I have consulted the documentation here, and my code contained in 'sendmessage.php' appears to be correct:

<?php
error_reporting(E_ALL);

echo getcwd();
echo function_exists('proc_open') ? "Yep, that will work" : "Sorry, that won't work";

require_once(dirname(__FILE__)."/swift.php");
echo 'Mail sent <br />';

// Grab the post data
$name = filter_var($_POST['Name'], FILTER_SANITIZE_STRING);
$usermail = filter_var($_POST['Email'], FILTER_SANITIZE_EMAIL);
$content = filter_var($_POST['Message'], FILTER_SANITIZE_STRING);

// Construct the body of the email
$data = "Name: " . $name . "<br />" . "Email: " . $usermail . "<br />" . "Message: " . $content;

// Create the transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
    ->setUsername('foobar@googlemail.com')
    ->setPassword('GENERATED PASSWORD');
echo 'line 26 <br />';

// Create the mailer using the created transport
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('This is the subject')
    ->setFrom(array('foobar@googlemail.com' => 'Feedback Received From User'))
    ->setTo(array('barfoo@gmail.com', 'foobar@googlemail.com' => 'Lead Recipients'))
    ->setSubject('Here is your Feedback subject')
    ->setBody($data, 'text/html');
echo 'line 34 <br />';

// Send the message
$result = $mailer -> send($message);
echo $result;
echo 'line 39<br />';
?>

proc_open returns a "Yep, that will work", but as above, I am not receiving the email in my inbox.

I have tried changing the port on the line in question to 25 and 486 with no success.

After a Google search I came across this question which seemingly had the same issue, but the accepted answer did not solve my problem.

Please note that I had a previous question regarding errors with a contact form, and this question follows on from that.

Community
  • 1
  • 1
tomdot
  • 551
  • 6
  • 20

1 Answers1

1

It seems that SwiftMailer is not loading correctly or fully. I can't guarantee it'll be any help, but try the following:

Do a GIT clone into your source (html or htdocs) directory (see https://github.com/swiftmailer/swiftmailer/blob/5.x/doc/installing.rst).

This should give you a directory called "swiftmailer", and the script you're loading swiftmailer from should be in the same directory. Then change your "require_once" line to the following:

require_once("swiftmailer/lib/swift_required.php");

I've only cast a cursory glance over the source for swiftmailer, but if I'm not mistaken it should do the rest for you automatically, including loading the Swift_SmtpTransport class.

SteJ
  • 1,491
  • 11
  • 13