Joshua
Joshua

Reputation: 694

Swift_Message not found error after swiftmailer installed in Yii2

I'm setting up a mailer in a project using Yii2. Swiftmailer seemed to be the best solution for this framework so I simply followed the docs.

In composer.json

 "yiisoft/yii2-swiftmailer": "*"

Followed by:

 composer install

In my config I have: (Yes, I just want it to save a file for now.)

'components' => [
    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'useFileTransport' => true,
    ],

Then I placed this into a controller:

 \Yii::$app->mailer->compose()
   ->setFrom('[email protected]')
   ->setTo('[email protected]')
   ->setSubject('Message subject')
   ->setTextBody('Plain text content')
   ->setHtmlBody('<b>HTML content</b>')
   ->send();

My error is:

PHP FATAL - Class 'Swift_Message' not found

I believe, after some research this has to do with the autoloader / lazy loading but the information I can find is spotty. Most of it states that if using composer to install resolves the issue, and all of my troubleshooting has left me chasing my tail.

Here's my stacktrace. Thank you for your help.

Additional:

  • composer-asset-plugin - IS INSTALLED
  • vendor/composer/autoload_files.php DOES contain the line:
  •  $vendorDir . '/swiftmailer/swiftmailer/lib/swift_required.php',  
    

    Upvotes: 2

    Views: 3239

    Answers (1)

    Joshua
    Joshua

    Reputation: 694

    I would love just close this embarrassment, but in case someone makes the same assumption as myself, i'd like to help.

    Does you entry script contains 'vendor/autoload.php' require? Does it point to the correct file? Try to manually put die('swiftmailer') at 'vendor/swiftmailer/swiftmailer/lib/swift_required.php' - it should appear at any application run.

    via (klimov-paul / yiisoft)

    die('swiftmailer'); had no effect.

    vendor/autoload.php is not required.

    I had assumed that the other extensions already installed in the vendor folder were in use. I came to find that previous developers were not utilizing anything there and require(__DIR__ . "/../vendor/autoload.php"); was not in the execution path.

    Upvotes: 1

    Related Questions