简体   繁体   English

Symfony2 FosuserBundle:重新发送注册确认电子邮件

[英]Symfony2 FosuserBundle: RE-send registration confirmation email

Framework PHP: Symfony2.6 框架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). 问题:我想在FosUserBundle中添加以下功能:“管理员可以将注册确认电子邮件重新发送给特定用户”(在网站的管理部分)。

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 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: 只需询问FOSUserBundlemailer并使用它重新发送邮件:

$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. 这将重新发送确认邮件的精确副本,因为使用了相同的FOSUserBundle代码。 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 config.ymlfos_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 极简主义者AcmeDemoBundle:提醒:remind.html.twig模板

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

Minimalist AcmeDemoBundle:Remind:email.html.twig template 极简主义AcmeDemoBundle:提醒:email.html.twig模板

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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