Search code examples
symfonyswiftmailer

Nonexistent parameter in Swiftmailer


In an attempt to configure both a memory & a spool mailer in a Symfony 4.3 application I followed the docs to create this configuration:

swiftmailer:
    default_mailer: memory
    mailers:
        memory:
            sender_address: 'admin@bogus.info'
            transport:        smtp
            username:         admin@bogus.info
            password:         123Abcd
            host:             localhost
            spool: { type: 'memory' }

        spooler:
                sender_address: 'admin@bogus.info'
                transport:        smtp
                username:         admin@bogus.info
                password:         123Abcd
                host:             localhost
                spool:
                    type: file
                    path: '%kernel.project_dir%/var/spool'

And in services.yaml:

    App\Services\Emailer:
        $spoolMailer: '%swiftmailer.mailer.spooler%'
        $defaultMailer: '%swiftmailer.default_mailer%'
        $senderAddress: '%swiftmailer.mailer.memory_mailer.sender_address%'
        $projectDir: '%kernel.project_dir%'

But with those four parameters in the service the following occurs with php bin/console debug:container:

The service "App\Services\Emailer" has a dependency on a non-existent parameter "swiftmailer.mailer.spooler"...

Why does this configuration not work?


Solution

  • The service "App\Services\Emailer" has a dependency on a non-existent parameter "swiftmailer.mailer.spooler"...

    Surrounding parameters with the % symbol allows you to pass values to your services.

    As you want to inject a service, you should prefix your parameter with the @ symbol.

    Also, to get the default mailer service, you have to inject @swiftmailer.mailer

    EDIT: Proper way to retrieve the sender address: %swiftmailer.mailer.memory.sender_address%


    Updated service definition :

    App\Services\Emailer:
      $spoolMailer:   '@swiftmailer.mailer.spooler'
      $defaultMailer: '@swiftmailer.mailer'
      $senderAddress: '%swiftmailer.mailer.memory.sender_address%'
      $projectDir:    '%kernel.project_dir%'