Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Swiftmailler发送相同的电子邮件两次_Php_Symfony_Swiftmailer - Fatal编程技术网

Php Swiftmailler发送相同的电子邮件两次

Php Swiftmailler发送相同的电子邮件两次,php,symfony,swiftmailer,Php,Symfony,Swiftmailer,我有一个问题,当我想在注册后发送电子邮件,我真的发送了一封电子邮件,但我发送了两次,不知道为什么, 这是我的代码,我创建了一个发送邮件的服务 我的邮递员: > public function emailUserPassword(UserInterface $user) { > $message = (new \Swift_Message('********')) > ->setContentType('text/html') >

我有一个问题,当我想在注册后发送电子邮件,我真的发送了一封电子邮件,但我发送了两次,不知道为什么, 这是我的代码,我创建了一个发送邮件的服务

我的邮递员:

> public function emailUserPassword(UserInterface $user) {
>     $message = (new \Swift_Message('********'))
>         ->setContentType('text/html')
>         ->setFrom('********')
>         ->setTo($user->getEmail())
>         ->setBody($this->twig->render('Mail/password.html.twig', array(
>             'user' => $user,
>         )));
>     $this->swiftmailler->send($message); }
我的控制器:

if (isset($lastName, $firstName, $email, $roles, $stations)) {
            $user = new User();
            try {
                $user->setFirstName($firstName);
                $user->setLastName($lastName);
                $user->setEmail($email);
                $user->setPassword($passwordEncoder->encodePassword($user, $passWordGenerator));
                $user->setRoles($roles);
                $user->setStations($stations->getValues());

                $entityManager->persist($user);
                $entityManager->flush();

                $event = new UserEvent($user);
                $this->eventDispatcher->dispatch(SecurityEvents::REGISTRATION_SUCCESS, $event);

            } catch (UniqueConstraintViolationException $e) {
                $dataError = [
                    'status' => 'errorException',
                    'message' => 'existe'
                ];
                return new JsonResponse($dataError, 500
                );
            }

            $dataValid = [
                'type' => 'Validation',
                'valid' => 'L\'utilisateur ' . $user->getFullName() . ' a été créé.'
            ];

            return new JsonResponse($dataValid, 201);
我的活动:

public function __construct(MailerHandler $mailerHandler
) {
    $this->mailerHandler = $mailerHandler;
}

public static function getSubscribedEvents()
{
    return [SecurityEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess'];
}

/**
 * @param UserEvent $event
 */
public function onRegistrationSuccess(UserEvent $event) {
    $user = $event->getUser();
    $this->mailerHandler->emailUserPassword($user);
}

我不明白为什么我会发送两封电子邮件

您是否尝试在事件处理程序中转储堆栈跟踪以查看它们的来源?您是否有EventSubscriber的服务配置?有时,侦听器/订阅者可能会意外注册两次。也许您可以检查注册了侦听器的EventDispatcher,例如,使用调试命令:
bin/console debug:event dispatcher
或web探查器工具栏我执行了您的命令,我有两次“App\EventListener\PassWordListener::onRegistrationSuccess()使用0优先级,我认为实现
EventSubscriberInterface
的服务会自动注册,您无需在服务中“再次”注册。Yamlohh谢谢您是的,我已在服务中声明了我的服务。yaml当我删除它时,我现在在symfony 3上发送1封邮件,我没有这个问题,是的,现在可以了,谢谢你们两个。你们有并没有尝试在事件处理程序中转储堆栈跟踪,看看它们来自哪里?你们有并没有为EventSubscriber配置服务?有时,侦听器/订阅者可能会意外注册两次。也许您可以检查注册了侦听器的EventDispatcher,例如,使用调试命令:
bin/console debug:event dispatcher
或web探查器工具栏我执行了您的命令,我有两次“App\EventListener\PassWordListener::onRegistrationSuccess()使用0优先级,我认为实现
EventSubscriberInterface
的服务会自动注册,您无需在服务中“再次”注册。Yamlohh谢谢您是的,我已在服务中声明了我的服务。yaml当我删除它时,我现在在symfony 3上发送1封邮件,我没有这个问题,好了,现在开始了谢谢你们两个