我想在fosuserbundle个人资料/编辑中编辑个人资料时收到确认电子邮件

时间:2016-02-24 01:27:35

标签: php symfony email

我希望收到一封电子邮件,确认我使用FOSUserBundle修改了个人资料/编辑内容。注册和密码重置的电子邮件确认工作正常。

这是我找到的代码,但我没有收到任何内容。

// src/OC/UserBundle/EventListener/ChangeProfileListener.php
namespace OC\UserBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ChangeProfileListener implements EventSubscriberInterface
{
    private $mailer;
    private $tokenGenerator;
    private $router;
    private $session;
    private $tokenStorage;

    public function __construct(
        MailerInterface $mailer,
        TokenGeneratorInterface $tokenGenerator,
        UrlGeneratorInterface $router,
        SessionInterface $session, TokenStorageInterface $tokenStorage
    ) {
        $this->mailer = $mailer;
        $this->tokenGenerator = $tokenGenerator;
        $this->router = $router;
        $this->session = $session;
        $this->tokenStorage = $tokenStorage;
    }

    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::PROFILE_EDIT_INITIALIZE => 'onProfileEditInitialize',
            FOSUserEvents::PROFILE_EDIT_SUCCESS => 'onProfileEditSuccess',
        );
    }

    public function onProfileEditInitialize(GetResponseUserEvent $event)
    {
        // required, because when Success's event is called, session already contains new email
        $this->email = $event->getUser()->getEmail();
    }

    public function onProfileEditSuccess(FormEvent $event)
    {
        $user = $event->getForm()->getData();
        if ($user->getEmail() !== $this->email)
        {
            // disable user
            $user->setEnabled(false);

            // send confirmation token to new email
            $user->setConfirmationToken($this->tokenGenerator->generateToken());
            $this->mailer->sendConfirmationEmailMessage($user);

            // force user to log-out
            $this->tokenStorage->setToken();

            // redirect user to check email page
            $this->session->set('fos_user_send_confirmation_email/email', $user->getEmail());
            $url = $this->router->generate('fos_user_registration_check_email');
            $event->setResponse(new RedirectResponse($url));
        }
    }
}

services.yml

parameters:
oc_user.email_change.listener.class: OC\UserBundle\EventListener\ChangeProfileListener

services:
  oc_user.email_change.listener:
      class: %oc_user.email_change.listener.class%
      arguments: ['@fos_user.mailer', '@fos_user.util.token_generator', '@router', '@session', '@security.token_storage']
      tags:
        - { name: kernel.event_subscriber }
app / Resources / FOSUserBundle / views / Registration / email.txt.twig 中的

{% block subject %}
Email Confirmation
{% endblock %}

{% block body_text %}

Welcome to example.com, {{ user.username }}!

To confirm your email, please follow this link: {{ confirmationUrl }}

If you received this e-mail in error just ignore this message. No further actions are required from you.

*****

See you soon!
{% endblock %}

提前感谢您的帮助!

0 个答案:

没有答案