注册后发送包含登录名和通行证的电子邮件 [英] Send email containing login and pass after registration

查看:46
本文介绍了注册后发送包含登录名和通行证的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在管理一个用于管理用户的Symfony2项目,我安装了功能正常的捆绑软件FosUserBundle,但是我找不到创建用户后立即发送包含用户名和密码的电子邮件的方法,因为我说我有两个用户:admin和用户类型,然后是admin的管理员将使用FOS注册形式的创建形式来创建用户,在两种类型的用户之间切换只是角色.

I am currently working on a Symfony2 project for managing user I installed the bundle FosUserBundle which is functional but I can not find how to send an email containing the username and password immediately after creating user, as I said I have two user: admin and user type, and it is the admin who will create the users with a form of creation that is the form of FOS Registration, changing between the two kind user is only the roles.

推荐答案

您可以连接到 EventDispatcher 并发送您自己的电子邮件,而不是使用您自己的 Listener由FOSUserBundle生成的电子邮件.

You could hook into the EventDispatcher and send your own email rather than than the one generated by FOSUserBundle using your own Listener.

class EmailConfirmationListener implements EventSubscriberInterface
{
    private $mailer;
    private $router;
    private $session;

    public function __construct(MailerInterface $mailer,
            UrlGeneratorInterface $router)
    {
        $this->mailer = $mailer;
        $this->router = $router;
    }

    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_SUCCESS => array(
                array('onRegistrationSuccess',  -10),
            ),
        );
    }

    public function onRegistrationSuccess(FormEvent $event)
    {
        /** @var $user \FOS\UserBundle\Model\UserInterface */
        $user = $event->getForm()->getData();

        // send details out to the user
        $this->mailer->sendCreatedUserEmail($user);

        // Your route to show the admin that the user has been created
        $url = $this->router->generate('blah_blah_user_created');
        $event->setResponse(new RedirectResponse($url));

        // Stop the later events propagting
        $event->stopPropagation();
    }
}

邮件服务

use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Mailer\Mailer as BaseMailer;

class Mailer extends BaseMailer
{
    /**
     * @param UserInterface $user
     */
    public function sendAdminConfirmationEmailMessage(UserInterface $user)
    {
        /**
         * Custom template using same positioning as
         * FOSUSerBundle:Registration:email.txt.twig so that the sendEmailMessage
         * method will break it up correctly
         */
        $template = 'BlahBlahUser:Admin:created_user_email.txt.twig';
        $url = $this->router->generate('** custom login path**', array(), true);
        $rendered = $this->templating->render($template, array(
            'user' => $user,
            'password' => $user->getPlainPassword(),
        ));
        $this->sendEmailMessage($rendered,
            $this->parameters['from_email']['confirmation'], $user->getEmail());
    }
}

我认为可以做到.尽管我可能错了.

I think that would do it.. although I could be wrong.

这篇关于注册后发送包含登录名和通行证的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆