且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

注册后发送包含登录名和通行证的电子邮件

更新时间:2023-12-01 17:46:10

您可以连接到 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.