FOSUserBundle 发送更多关于用户创建的邮件

FOSUserBundle sending more email on user creation

我正在使用 Symfony2 开发一个网站,我正在使用 FosUserBundle 来管理用户。

关于用户注册,我想给管理员发邮件通知活动。 我遵循了官方 FOS manual 但我无法正确覆盖邮件发送,而且我不知道如何为管理员生成新电子邮件。

第一个问题来自 config.yml 文件,我在其中为 Mailer 覆盖设置了以下参数

# ...
fos_user:
        registration:
            confirmation:
                enabled: true
            email:
                template: UserBundle:Registration:confirmation.email.twig
            form:
                type: user_registration

email:template: 行给我以下错误:

[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]  
Unrecognized option "email" under "fos_user.registration"

请问如何解决这个问题,以及如何实现二次发送邮件?

documentation here 中,它展示了如何覆盖注册控制器以记录用户注册事件。将电子邮件发送给您的管理员而不是创建日志条目应该不需要太多更改。

另见 how to send an email

对于类似的情况,我使用了 EventListener 来发送电子邮件。邮件程序是一个服务,也如下所示。

听众

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityManager;

/**
 * Description of RegistrationListener
 *
 * @author George
 */
class RegistrationListener implements EventSubscriberInterface
{

    private $em;
    private $mailer;
    private $tools;

    public function __construct(EntityManager $em, $mailer, $tools)
    {
        $this->em = $em;
        $this->mailer = $mailer;
        $this->tools = $tools;
    }

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

    /**
     * Persist organization on staff registration success
     * @param \FOS\UserBundle\Event\FormEvent $event
     */
    public function onRegistrationSuccess(FormEvent $event)
    {
        /** @var $user \FOS\UserBundle\Model\UserInterface */
        $user = $event->getForm()->getData();
        $user->setAddDate(new \DateTime());
        $type = $this->tools->getUserType($user);
        if ('staff' === $type) {
            $organization = $user->getOrganization();
            $organization->setTemp(true);
            $user->setOrganization($organization);
            $this->em->persist($organization);
            $user->addRole('ROLE_STAFF');
            $this->mailer->sendNewOrganization($organization);
        }
        if ('admin' === $type) {
            $user->addRole('ROLE_ADMIN');
        }
        if ('volunteer' === $type) {
            $user->setReceiveEmail(true);
            $user->setEnabled(true);
        }
    }

}

邮件服务

use \Symfony\Component\DependencyInjection\ContainerAware;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Mailer\MailerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Doctrine\ORM\EntityManager;

/**
 * Description of AdminMailer
 *
 * @author George
 */
class AdminMailer extends ContainerAware implements MailerInterface
{

    protected $mailer;
    protected $router;
    protected $twig;
    protected $parameters;
    protected $em;

    public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters, EntityManager $em)
    {
        $this->mailer = $mailer;
        $this->router = $router;
        $this->twig = $twig;
        $this->parameters = $parameters;
        $this->em = $em;
    }

    ...other functions

    /**
     * Alert admins to new org being created
     * @param type $organization
     * @return type
     */
    public function sendNewOrganization($organization)
    {
        $message = \Swift_Message::newInstance()
                ->setSubject('New organization')
                ->setFrom($this->parameters['address'])
                ->setTo($this->adminRecipients())
                ->setContentType('text/html')
                ->setBody(
                $this->twig->render(
                        'new_org', array(
                    'organization' => $organization,
                        ), 'text/html'
                )
                )
        ;

        return $this->mailer->send($message);
    }


protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
{
    $context = $this->twig->mergeGlobals($context);
    $template = $this->twig->loadTemplate($templateName);
    $subject = $template->renderBlock('subject', $context);
    $textBody = $template->renderBlock('body_text', $context);
    $htmlBody = $template->renderBlock('body_html', $context);

    $message = \Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom($fromEmail)
            ->setTo($toEmail);

    if (!empty($htmlBody)) {
        $message->setBody($htmlBody, 'text/html')
                ->addPart($textBody, 'text/plain');
    }
    else {
        $message->setBody($textBody);
    }

    $this->mailer->send($message);
}


}

services.yml(节选)

truckee.registration_listener:
    class: Truckee\VolunteerBundle\EventListener\RegistrationListener
    arguments: 
        em: @doctrine.orm.entity_manager
        mailer: @admin.mailer
        tools: @truckee.toolbox
    tags:
        - { name: kernel.event_subscriber }
//toolbox gets user type (amongst other functions)
truckee.toolbox:
    class: Truckee\VolunteerBundle\Tools\Toolbox
    arguments: [@doctrine.orm.entity_manager]
//admin mailer sends lots of different e-mail messages
admin.mailer:
    class: Truckee\VolunteerBundle\Tools\AdminMailer
    arguments:
        - '@mailer'
        - '@router'
        - '@twig'
        -
            sandbox: %sandbox%
            address: %admin_email%
            template:
                confirmation: '%fos_user.registration.confirmation.template%'
                resetting: '%fos_user.resetting.email.template%'
            from_email:
                confirmation: '%fos_user.registration.confirmation.from_email%'
                resetting: '%fos_user.resetting.email.from_email%'
        - '@doctrine.orm.entity_manager'