1

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

jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22

1 Answers1

0

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

knetsi
  • 1,601
  • 1
  • 16
  • 18
  • hello! It is neccessary to have any configuration of swift mailer? because i have this in my .env file (symfony 3.4) MAILER_URL=smtp://localhost:25?encryption=ssl&auth_mode=login&username=MYEMAIL&password=MyPasss – Diego Bastidas Jan 14 '18 at 00:12
  • yes , by default fosuserbundle is using swiftmailer, also you could skip the fosuserbundle mail function and send a custom email , by generating a link with the confirmation token – knetsi Jan 14 '18 at 00:18
  • thanks for yout answer, but it does not work yet. i have called the mailer's service this way: $mailer = $this->get('fos_user.mailer'); then, i send my message like this: $mailer->sendConfirmationEmailMessage($user); but does not works. D: can you help me? https://stackoverflow.com/questions/48251525/emailconfirmation-with-fosuserbundle-does-not-work – Diego Bastidas Jan 14 '18 at 17:24
  • First of all, do you get any errors? The `$mailer = $this->get('fos_user.mailer');` should not provide you the service by default, did you add it in your services.yml file? Do you use the symfony autowiring services(https://symfony.com/doc/3.4/service_container/autowiring.html)? Also , yours tests are made from the development enviroment? Do you have enabled the `swiftmailer.disable_delivery` configuration? How do you check that it is not working, you get any error or you just checking if the e-mail was sent? Because the mail may be blocked by the user's mail server. – knetsi Jan 14 '18 at 17:33
  • also in the new question you asked, you did not mentioned the custom register controller, does that mean you abandoned that idea? Also please note that you can override any part of a Bundle, take a look at http://symfony.com/doc/3.4/bundles/override.html and http://symfony.com/doc/3.4/templating/overriding.html – knetsi Jan 14 '18 at 17:38
  • i have added $user->setConfirmationToken($this->tokenGenerator->generateToken());$this->mailer->sendConfirmationEmailMessage($user); in my custom controller as you told me, and does not work, so i tried to made the validation with the FosUser's Registrationcontroller, and doest not work too. so i thoug it is some mistake in the swiftmailer's configuration – Diego Bastidas Jan 14 '18 at 17:52