how to send and emailconfirmation since my own controller with FOS USER BUNDLE?

I know that fosUserBundle from Symfony can send EmailConfirmation automactly, just adding:

fos_user:
# ...
registration:
    confirmation:
        enabled: true

but i have my own register's form in my own controller. (I am NOT using register's form of FOSUSER). and i don't know how to send an emailconfirmation when an user register in my system.

these are my register's functions I am using:

    public function index(Request $request)
{
    $customer = new Customer();
    $form = $this->createForm(CustomerType::class, $customer);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $pass = $form->get('password')->getData();
        $email = $form->get('email')->getData();
        $this->register($email, $email, $pass);
        $em->persist($customer);
        $em->flush($customer);
        header("Refresh:0");
    }
    return $this->render('backend/customer/customer_register.html.twig', array('form' => $form->createView()));
}

and this Auxiliar Function:

    private function register($email, $username, $password)
    {
    $userManager = $this->get('fos_user.user_manager');
    $email_exist = $userManager->findUserByEmail($email);
    if ($email_exist) {
        return false;
    }
    $user = $userManager->createUser();
    $user->setUsername($username);
    $user->setEmail($email);
    $user->setEmailCanonical($email);
    $user->setEnabled(1);
    $user->setPlainPassword($password);
    $user->addRole('ROLE_CUSTOMER');
    $userManager->updateUser($user);
    return true;
    }

I have tried using

        $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');

but doesn't work

Please, I don't know how to start. it is neccessary to have any configuration of swift mailer?

if someone can help me I would appreciate it very much

Thanks, Thank you very much

1 answer

  • answered 2018-01-13 21:29 knetsi

    You can take a look at the code used in FOSuserbundle at Controller/RegistrationController::registerAction() dispatches an event FOSUserEvents::REGISTRATION_SUCCESS which calls the function EventListener/EmailConfirmationListener::onRegistrationSuccess() It generates a token and set it to the user Entity $user->setConfirmationToken($this->tokenGenerator->generateToken()); then sends the email $this->mailer->sendConfirmationEmailMessage($user);

    note the mailer is instance of FOS\UserBundle\Mailer\MailerInterface and the token generator FOS\UserBundle\Util\TokenGeneratorInterface