简体   繁体   中英

Symfony2 FosuserBundle: RE-send registration confirmation email

Framework PHP: Symfony2.6

Problem: I would like to add the following functionality to FosUserBundle: "The admin can re-send the registration confirmation email to a specific user" (in the admin section of the website).

I've already built the "user details" page where the admin can see all the information entered in the registration form and if the the user is enabled/confirmed. If the user is not enabled I will add a button to re-send the confirmation email.


Another solution is to to display a link to the user, after he tries to login with credentials that are not confirmed. Here is a similar question (that unfortunately has no feedback and it's not very clear to me and only covers the second approach):

https://stackoverflow.com/questions/25204877/re-sending-confirmation-email-fosuserbundle

Can you please point me towards the easiest and quickest solution?

I know this an old question but today I came across the same problem and I found a simpler solution. Maybe this also helpful to others:

Simply ask the FOSUserBundle for its mailer and use it to re-send the message:

$mailer = $this->get('fos_user.mailer');                    
$mailer->sendConfirmationEmailMessage($user);

That's it! This will re-send an exact copy of the confirmation mail, since the same FOSUserBundle code is used. No need to manually re-create the message.

Here is a shot at what it takes. Assumptions:

  • in config.yml , fos_user.service.mailer: fos_user.mailer.twig_swift
  • user email is known

Controller

/**
 * @Route("/remind")
 *
 */
    class RemindController extends Controller
    {
        /**
         * @Route("/{email}")
         * @Template()
         */
        public function remindAction($email)
        {
            $user = $this->get('fos_user.user_manager')->findUserByEmail($email);
            $url = $this->generateUrl('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), true);

            $message = \Swift_Message::newInstance()
                    ->setSubject('Registration confirmation')
                    ->setFrom('admin@acmedemo.com')
                    ->setTo($email)
                    ->setContentType('text/html')
                    ->setBody(
                    $this->renderView(
                            "AcmeDemoBundle:Remind:email.html.twig", array(
                        'user' => $user,
                        'confirmationUrl' => $url))
                    )
            ;
            $sent = $this->get('mailer')->send($message);

            return ['user' => $user, 
                'url' => $url,
                'success' => $sent ? 'Yes' : 'No'];
        }
    }

Minimalist AcmeDemoBundle:Remind:remind.html.twig template

{{ user.email }}<br>
{{ url }}<br>
{{ success }}

Minimalist AcmeDemoBundle:Remind:email.html.twig template

Please confirm your registration by visiting <a href="{{ confirmationUrl }}">this link</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM