【问题标题】:FOSUserBundle sending more email on user creationFOSUserBundle 在用户创建时发送更多电子邮件
【发布时间】:2015-07-17 07:24:04
【问题描述】:

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

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

【问题讨论】:

  • 您没有正确遵循“手册”。在fos_user.registration 下没有email 选项,email 选项在fos_user.resetting 下。
  • 是的,也许我没有完全按照手册进行操作。我正在尝试 GeoB 的解决方案,听起来不错!

标签: email symfony fosuserbundle


【解决方案1】:

对于类似的情况,我使用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'

【讨论】:

  • 我太喜欢你的解决方案了。您在 service.yml 的“arguments”参数上添加了什么?
  • 我已经编辑了我的答案以包含来自services.yml 的部分。那里有一些对我的应用程序来说是独一无二的。希望这会有所帮助。
  • 非常感谢!我得到了它! :D
【解决方案2】:

documentation here 中,它显示了如何覆盖注册控制器来记录用户注册事件。无需太多更改即可向您的管理员发送电子邮件而不是创建日志条目。

另见how to send an email

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    相关资源
    最近更新 更多