score:2

Accepted answer

For a similar situation I've used an EventListener to send an e-mail. The mailer is a service, which is also shown below.

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);
        }
    }

}

mailer service

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 (excerpts)

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'

score:1

In the documentation here it shows how you can override the registration controller to log a user registration event. It shouldn't take much to change that to send an email to your admins instead of creating a log entry.

See also how to send an email.


Related Query

More Query from same tag