如何使用Silex 2.x和Swiftmailer?

时间:2017-11-07 16:10:57

标签: silex 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

不知道我哪里错了!?

1 个答案:

答案 0 :(得分:1)

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