FOSUserBundle在用户创建时发送更多电子邮件

时间:2015-05-06 22:38:04

标签: email symfony fosuserbundle

我正在使用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"

关于如何解决问题以及如何实施第二次邮件发送的任何其他建议?

2 个答案:

答案 0 :(得分:2)

对于类似情况,我使用EventListener发送电子邮件。邮件是一项服务,如下所示。

listenter

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'

答案 1 :(得分:1)

documentation here中,它显示了如何覆盖注册控制器以记录用户注册事件。将更改信息发送给管理员而不是创建日志条目不应该花费太多时间。

另见how to send an email