1

While it is possible to create an environment variable SENDER_ADDRESS and access that in a controller, the address is not available in a functional test. This is due to the \tests directory being at the same level as \src. In an environment service EnvService:

use Symfony\Component\Dotenv\Dotenv;

class EnvService
{
    public function mailSender() {
        $dotenv = new Dotenv();
        $dotenv->load('../.env');
        $sender = getenv('SENDER_ADDRESS');

        return $sender;
    }
}

then in a controller:

    public function invite(Request $request, \Swift_Mailer $mailer, EnvService $env)
    {
        $invite = new Invitation();
        $form = $this->createForm(InvitationType::class, $invite);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $invitation = $request->request->get('invitation');
//            $sender = $this->get('mailer.sender_address');
            $sender = $env->mailSender();
...

the email will be sent.

But in a functional test this error occurs:

Unable to read the "../.env" environment file

If instead swiftmailer.yaml is modified to includesender_address: 'admin@bogus.info' the error

Service "swiftmailer.sender_address" not found: the container inside "App\Controller\RegistrationController" is a smaller service locator

I've tried multiple variations on the theme of adding a SwiftMailer service in services.yaml without success. The most recent iteration is:

swiftmailer:
    class: 'Swift_Mailer\Swift\Mailer'
    sender_address: 'admin@bogus.info'
    public: true
geoB
  • 4,578
  • 5
  • 37
  • 70
  • I find your `EnvService` slightly obsolete. Have you tried using the `ParameterBagInterface` like here: https://symfony.com/blog/new-in-symfony-4-1-getting-container-parameters-as-a-service (under the assumption you're using symfony >= 4.1) Environments are handled pretty good by symfony already, why rewrite it? – Jakumi Mar 31 '19 at 02:17
  • "slightly obsolete" - just like me! The app is actually a hybrid - a new 3.4 project that's using the v4 directory structure. I tried the `ParameterBagInterface ` but got `...Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface" but no such service exists`. – geoB Mar 31 '19 at 18:43
  • yeah, it was introduced in symfony 4.1 I believe, sorry ;o/ but in your controller in 3.4 I believe you can just `$this->container->getParameter()` look up the controller trait, i think it's in there. – Jakumi Mar 31 '19 at 18:58
  • `$this->container->getParameter()` is an unknown method. `$this->container->get()` throws the `smaller service locator` error. – geoB Mar 31 '19 at 19:17
  • there was a way to get parameters ... – Jakumi Mar 31 '19 at 19:19
  • according to https://symfony.com/doc/3.4/service_container/parameters.html this should be correct ;o/ ... so you're apparently half way into 4 – Jakumi Mar 31 '19 at 19:27
  • It is worth noting that the 4.2 version of the same app allows accessing `swiftmailer.sender_address` in `swiftmailer.yaml`. I've just been fiddling with 3.4 to do an LTS version of the app. – geoB Mar 31 '19 at 21:40
  • I believe it's another case of "you can't get there from here". The version of the app built from `composer create-project symfony/website-skeleton` uses v4 of `sensio/framework-extra-bundle`. I have a version of the app built from now obsolete `https://symfony.com/installer` that uses v3. And in this older app the swiftmailer parameter is available. I'll stick with the older app. – geoB Apr 01 '19 at 21:22

0 Answers0