Php 在fosuserbundle profile/edit中编辑配置文件时,我希望获得电子邮件确认
我想收到一封电子邮件,确认我在个人资料/编辑中使用的修改。注册确认和密码重置的电子邮件工作正常 这是我找到的密码,但我没有收到任何东西Php 在fosuserbundle profile/edit中编辑配置文件时,我希望获得电子邮件确认,php,symfony,email,Php,Symfony,Email,我想收到一封电子邮件,确认我在个人资料/编辑中使用的修改。注册确认和密码重置的电子邮件工作正常 这是我找到的密码,但我没有收到任何东西 // src/OC/UserBundle/EventListener/ChangeProfileListener.php namespace OC\UserBundle\EventListener; use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\Event\GetResponseUserEvent;
// 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));
}
}
}
服务.yml
在app/Resources/FOSUserBundle/views/Registration/email.txt.twig中
提前感谢您的帮助 你在日志中看到错误了吗?没有,我什么都没有。甚至没有消息告诉我,我将收到一封电子邮件,以确认我的更改。我只收到一条消息,我的更改已经更新。你能调试一下吗?您是否使用了
onProfileEditSuccess()
方法?当你改变你的个人资料时,你会进入if($user->getEmail()!==$this->email){
语句吗?你可以简单地使用die()
调试。很抱歉,我不太明白如何调试它?这是我在symfony的第一个项目,所以我不太擅长。这种类型的调试与symfony无关,但很常见。你应该在onProfileEditSuccess()
方法的开头添加die或function,例如die('it works!')
。然后,如果您更改配置文件并看到它可以工作!
文本-您将进入onProfileEditSuccess()
方法。
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 }
{% 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 %}