Search code examples
symfonyfosuserbundlesymfony-3.4

Symfony Fosuserbundle resend email


I am trying to make a link that resends a confirmation token to a user after registering in Symfony3.

However, I get a deprecation message as follows:

User Deprecated: The "fos_user.mailer" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead.

Here is my controller:

public function resendActivationEmail($token, UserManagerInterface $userManager)
{
    $user = $userManager->findUserByConfirmationToken($token);
    if (is_null($user)) {return $this->redirectToRoute('fos_user_registration_register');}
    $mailer = $this->get('fos_user.mailer');
    $mailer->sendConfirmationEmailMessage($user);
    return $this->redirectToRoute('fos_user_registration_check_email');
}

My services.yml:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: truesubscribers, etc.
        autoconfigure: true
        public: false

I looked into the docs, it says that in Symfony3.4, services are private by default. I am using autowiring in my app, so how I get the fos_user.mailer without any deprecation warnings?

I tried setting Fosuserbundle services to public, doesnt help:

services.yml:

    ....
    FOS\UserBundle:
            public: true

Any help appreciated!


Solution

  • It's better to use DependencyInjection instead of call container directly. You should pass your mailer to your method:

    public function resendActivationEmail($token, UserManagerInterface $userManager, \FOS\UserBundle\Mailer\MailerInterface $mailer)
    {
        $user = $userManager->findUserByConfirmationToken($token);
        if (is_null($user)) {return $this->redirectToRoute('fos_user_registration_register');}
        $mailer->sendConfirmationEmailMessage($user);
        return $this->redirectToRoute('fos_user_registration_check_email');
    }
    

    For more informations about dependencyInjection : https://symfony.com/doc/3.4/service_container/injection_types.html