0

I am implementing a mailing solution where if I call swiftmailer from the main index.php file like :

require_once '../swiftmailer/lib/swift_required.php';
...
$result = $mailer->send($message);

All is great. But when I use the same swiftmailer library in my application class to implement layout/design partition like this:

// File: controller.php

class Index {
      ...
      public function ignition() {
            ...
            spl_autoload_register(array($this, 'myClassLoaderFx'));
      }

      public function myClassLoaderFx($name) {
            $classes = array(
                  'Blah' => '../blah/Blah.php',
                  ...
                  'Swift' => '../swiftmailer/lib/swift_required.php'
            );
            if(!array_key_exists($name, $classes)){
                  die('Class not found');
            }
            require_once $classes[$name];
      }

// File: Mailer.php

class Mailer {
      ...
      public function main($recepient[], $sender, $subject, $body) {
            ...
            $transport = Swift_SmtpTransport::newInstance('xyz.google.com', 465, "ssl")->setUsername($this->username)->setPassword($this->password);
            ...
            $result = $mailer->send($message);
}

Upon execution I get : Class "Swift_SmtpTransport" not found. The library is loaded because I get all Swift class list in the netbeans context help. But, upon execution I get the error. I have done all possible spell checkings. Getting nowhere from here on. Please help.

Martin Gunnarsson
  • 159
  • 1
  • 1
  • 11
  • The answer over here: http://stackoverflow.com/questions/29626658/fatal-error-class-swift-smtptransport-not-found-in used "Swift_SmtpTransport::newyInstance()" I'm not usre if the y is a typo or if that you need that y for it to work. – Peavey2787 Oct 08 '16 at 05:56
  • I have checked for a typo and also checked for function names. Not reached anywhere. Thanks. – Angle Speak Oct 08 '16 at 05:59

1 Answers1

1

Looks like your swiftmailer class Swift() is loaded but its incomplete. You can take one of the following approaces to solve this :

  1. Add "extends Swift" to your class declaration in file Mailer.php.

    class Mailer extends Swift {

Or,

  1. Load the swiftmailer library in a construct() function of Mailer class.

    Remove the following line from myClassLoaderFx() function from controller.php : 'Swift' => '../swiftmailer/lib/swift_required.php'

    And Create a construct for Mailer() class in file Mailer.php public function __construct(){ require_once '../swiftmailer/lib/swift_required.php'; }

Go with the first approach of using spl_autoload_ function unless you want to keep swiftmailer away from the main application into a managed class. This can come in handy in case you would like to switch to another mailer. You wont have to make changes to controller.php etc.