Search code examples
emailsymfonyregistrationfosuserbundleconfirmation

FOSUser Bundle manual trigger confirmation email


I use the FOSUser Bundle as a basis class for my own User class. Through a webservice it's possible to create new users.

As far as I know it's only triggered when a user registers himself through the registration form.

Is there a way to manual trigger the confirmation email from a controller?


Solution

  • It seems like there is no solution to manually trigger the dispatch of the confirmation email. It's only triggered in combination with the registration form. What I ended up doing is to fake a form and the dispatch of it to trigger the event listener responsible for the dispatch of the email.

    //use FOS\UserBundle\FOSUserEvents;
    //use FOS\UserBundle\Event\FormEvent;
    
    $formFactory = $this->get('fos_user.registration.form.factory');
    $form = $formFactory->createForm();
    $form->setData($user); // created user object
    $event = new FormEvent($form, $request); // request of the Controller
    $dispatcher = $this->get('event_dispatcher');
    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
    

    Not really a clean solution, but the only one I could come up with, as the FOSUserBundle doesn't seem to offer any kind of API for it.