Symfony 4在服务中访问Swift_Mailer

时间:2018-11-13 01:05:54

标签: symfony swiftmailer symfony4

我一直在查看有关使用Swift_mailer的Symfony 4.1文档。但是,看来文档仅是假定它在Controller类中使用。我正在尝试创建具有一些可重用功能的服务,以发送电子邮件。

我在服务目录中创建了一个EmailService.php文件。创建此服务的新实例时,它会迅速抛出错误:

  

“函数参数太少   App \ Service \ EmailService :: __ construct(),传入0   *第33行的MyApp \ src \ Controller \ TestController.php   恰好有1个期望值”。

我不确定如何正确地将\ Swift_Mailer $ mailer传递给__construct?我在services.yaml中启用了自动接线,所以我不确定该怎么做?

class EmailService
{
    private $from = 'support@******.com';
    private $mailer;

    public function __construct(\Swift_Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

如何将\ Swift_Mailer传递到此EmailService构造中?

我尝试将其添加到config \ services.yaml中,但未成功:

App\Service\EmailService:
        arguments: ['@mailer']

2 个答案:

答案 0 :(得分:0)

正如dbrumann在评论中提到的那样,我需要遵循正确的注入服务方式。

首先,我需要将服务添加到config / services.yaml

#config/services.yaml
emailservice:
    class: App\Service\EmailService
    arguments: ['@swiftmailer.mailer.default', '@twig']
    public: true

第二,我需要将服务设置为同时接受邮件程序和树枝以呈现模板。

#App/Service/EmailService.php
<?php
namespace App\Service;

class EmailService
{
    private $from = 'support@*****.com';
    private $mailer;
    private $templating;

    public function __construct(\Swift_Mailer $mailer, \Twig\Environment $templating)
    {
        $this->mailer       = $mailer;
        $this->templating   = $templating;
    }
   public function userConfirmation(string $recipient, string $confCode) : bool
   {

        $message = (new \Swift_Message())
        ->setSubject('Some sort of string')
        ->setFrom($this->from)
        ->setTo($recipient)
        ->setBody(
            $this->templating->render(
                'email/UserConfirmation.html.twig',
                array('confCode' => $confCode)
            ),
            'text/html'
        )
        /*
         * If you also want to include a plaintext version of the message
        ->addPart(
            $this->renderView(
                'emails/UserConfirmation.txt.twig',
                array('confCode' => $confCode)
            ),
            'text/plain'
        )
        */
    ;
    return $this->mailer->send($message);
  }
}

第三,要从控制器调用它,请确保您的控制器扩展了 Controller 而不是 AbstractController !至关重要的一步!这是一个基于我在服务中需要的参数的示例:

public function userConfirmation()
{
     $emailService   = $this->get('emailservice');
     $sent = $emailService->userConfirmation('some@emailaddress.com', '2ndParam');
     return new Response('Success') //Or whatever you want to return
 }

我希望这对人们有帮助。 AbstractController无法为您提供对服务容器的正确访问。

答案 1 :(得分:0)

尝试:

#config/services.yaml

    App\Service\EmailService
        arguments: ['@swiftmailer.mailer.default', '@twig']
        public: true

在您的控制器中:

public function userConfirmation(EmailService $emailService)
{    
     $sent = $emailService->userConfirmation('some@emailaddress.com', '2ndParam');
     return new Response('Success') //Or whatever you want to return
 }

您使用symfony 4.1,因此不再调用控制器中的服务容器... https://symfony.com/doc/current/service_container/3.3-di-changes.html

您还可以使用FQCN“ App \ Service \ MyService”在services.yaml中声明服务,并使用适当的legacy_aliases.yaml文件声明诸如“ app.service.my.service”之类的旧别名,这有助于保留services.yaml我认为干净...