Symfony EventListener实现注册成功后给用户发送邮件 (fosuserbundle)

事件的定义在 FOS\UserBundle\FOSUserEvents里面
跟注册有关的是:

const REGISTRATION_INITIALIZE = 'fos_user.registration.initialize';
const REGISTRATION_SUCCESS = 'fos_user.registration.success';
const REGISTRATION_COMPLETED = 'fos_user.registration.completed';
const REGISTRATION_CONFIRM = 'fos_user.registration.confirm';
const REGISTRATION_CONFIRMED = 'fos_user.registration.confirmed';

选取FOS\UserBundle\FOSUserEvents::REGISTRATION_SUCCESS这个事件来发送邮件

request = $request ;
        $this->doctrine = $doctrine;
    }

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

    public function onRegistrationSuccess($event)
    {

        $name = $event->getForm()->getData()->getName();
                $email = $event->getForm()->getData()->getEmail();
                $this->sender->send( $email , $name , $this->template->welcome() );
    }

}

$sender是自己写的一个发送service $template是生成邮件模板的service

然后再service中加入

    mot_user.registration_success:
        class: Mot\User\EventListener\RegistrationSuccessListener
        arguments: [@request_stack , @my_sender , @my_template]
        tags:
            - { name: kernel.event_subscriber }

ok done 在注册form submit成功之后 会发送欢迎邮件到用户那边

你可能感兴趣的:(symfony)