如何使用 Silex 2.x 和 Swiftmailer?

How to use Silex 2.x and Swiftmailer?

我正在尝试在 Silex 中使用 Swiftmailer。 添加了 Swiftmailer:

composer require swiftmailer/swiftmailer

// Swiftmailer
$app->register(new Silex\Provider\SwiftmailerServiceProvider());

// $app['swiftmailer.use_spool'] = false;

$app['swiftmailer.options'] = [
    'host'       => 'localhost',
    'port'       => '25',
    'username'   => '',
    'password'   => '',
    'encryption' => null,
    'auth_mode'  => null
];

这是我的部分路线:

$message = \Swift_Message::newInstance()
    ->setSubject('[YourSite] Feedback')
    ->setFrom(['noreply@yoursite.com'])
    ->setTo(['feedback@yoursite.com'])
    ->setBody("test");

$app['mailer']->send($message);

return new Response('Thank you for your feedback!', 201);

一切都按照文档完成但是,这里有一个错误:

Fatal error: Uncaught Error: Call to undefined method Swift_Message::newInstance() in D:\OSPanel\domains\*************\routes\routes.php:94 Stack trace: #0 [internal function]: {closure}(Object(Symfony\Component\HttpFoundation\Request)) #1 D:\OSPanel\domains\*************\vendor\symfony\http-kernel\HttpKernel.php(153): call_user_func_array(Object(Closure), Array) #2 D:\OSPanel\domains\*************\vendor\symfony\http-kernel\HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #3 D:\OSPanel\domains\*************\vendor\silex\silex\src\Silex\Application.php(496): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #4 D:\OSPanel\domains\*************\vendor\silex\silex\src\Silex\Application.php(477): Silex\Application->handle(Object(Symfony\Component\HttpFoundation\Request)) #5 D:\OSPanel\domains\*************\web\index.php(5): Silex\Application->run() #6 {main} thrown in D:\OSPanel\domains\*************\routes\routes.php on line 94

不知道我哪里错了!?

感谢 mtorres 的提示。 Silex 2 仅使用 Swiftmailer 5.x