2

我目前正在开发一个用于管理用户的 Symfony2 项目我安装了功能性的捆绑包 FosUserBundle,但我找不到如何在创建用户后立即发送包含用户名和密码的电子邮件,正如我所说我有两个用户:管理员和用户类型,由管理员创建用户,创建形式为 FOS 注册形式,两种用户之间的切换只是角色。

4

4 回答 4

7

您可以连接EventDispatcher并发送您自己的电子邮件,而不是 FOSUserBundle 使用您自己的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());
    }
}

我认为那会做到这一点..虽然我可能是错的。

于 2013-07-20T12:02:15.377 回答
3

它记录在包的文档中:

// src/Acme/UserBundle/Controller/RegistrationController.php
<?php

namespace Acme\UserBundle\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;
use FOS\UserBundle\Controller\RegistrationController as BaseController;

class RegistrationController extends BaseController
{
    public function registerAction()
    {
        $form = $this->container->get('fos_user.registration.form');
        $formHandler = $this->container->get('fos_user.registration.form.handler');
        $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');

        $process = $formHandler->process($confirmationEnabled);
        if ($process) {
            $user = $form->getData();

            /*****************************************************
             * Add new functionality (e.g. log the registration) *
             *****************************************************/
            $this->container->get('logger')->info(
                sprintf('New user registration: %s', $user)
            );

            if ($confirmationEnabled) {
                $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
                $route = 'fos_user_registration_check_email';
            } else {
                $this->authenticateUser($user);
                $route = 'fos_user_registration_confirmed';
            }

            $this->setFlash('fos_user_success', 'registration.flash.user_created');
            $url = $this->container->get('router')->generate($route);

            return new RedirectResponse($url);
        }

        return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
            'form' => $form->createView(),
        ));
    }
}
于 2013-07-19T11:30:36.180 回答
1

只需使用模板和翻译器

配置

fos_user:
    registration:
       form:
           template:   AppBundle:Registration:email.txt.twig

模板

{% trans_default_domain 'FOSUserBundle' %}
{% block subject %}
{%- autoescape false -%}
{{ 'registration.email.subject'|trans({'%username%': user.username, '%confirmationUrl%': confirmationUrl}) }}
{%- endautoescape -%}
{% endblock %}

{% block body_text %}
{% autoescape false %}
{{ 'registration.email.message'|trans({'%username%': user.username, '%confirmationUrl%': confirmationUrl, %userpassword%: user.plainPassword}) }}
{% endautoescape %}
{% endblock %}
{% block body_html %}{% endblock %}

翻译app\Resources\translations\FOSUserBundle.en.yml

registration:
    check_email: |
        An email has been sent to %email%. It contains an activation link you must click to activate your account.
    confirmed: 'Congrats %username%, your account is now activated.'
    back: 'Back to the originating page.'
    submit: Register
    flash:
        user_created: 'The user has been created successfully.'
    email:
        subject: 'Welcome %username%!'
        message: |
            Hello %username%!

            To finish activating your account - please visit %confirmationUrl%

            User name: %username%
            Password: %userpassword%

            This link can only be used once to validate your account.

            Regards,
            the Team.
于 2017-08-11T09:46:30.150 回答
0

以下是使用 symfony2 注册后如何向用户发送用户名/电子邮件和密码:

将 email.txt.twig覆盖到您自己的包中,然后添加此电子邮件正文:

{% block subject %}
    Welcome to My Application
{% endblock %}

{% block body_html %}
    Email : {{ user.email }} 
    Username : {{ user.username }} 
    Password : {{ user.plainPassword }} 
    Activation Link : {{confirmationUrl}} 
{% endblock %}

然后电子邮件将发送到用户的电子邮件,其中包含电子邮件、用户名、密码和激活 URL 以激活帐户。

于 2015-01-12T11:17:57.647 回答