Symfony2 and FOSUserBundle : Override Mailer


Symfony2 and FOSUserBundle : Override Mailer

我来这里是因为我的OVH和我的symfony2应用程序有问题,我不能用SwiftMailer发送邮件。FOSUserBundle在登录后重置密码或激活我的帐户是相当有问题的。所以我想使用在PHP中实现的MAIL函数,我遵循了这里的文档。所以我创建了我的类MailerInterface.php和类Mailer.php,因为这个类使用函数sendEmailMessage 发送邮件

在这里,你可以看到我是如何实现类的:

<?php
      namespace MonApp'UserBundle'Mailer;
      use FOS'UserBundle'Model'UserInterface;
      interface MailerInterface
      {
           function sendConfirmationEmailMessage(UserInterface $user);
           function sendResettingEmailMessage(UserInterface $user);
      }
?>

Mailer.php:

<?php
      namespace MonApp'UserBundle'Mailer;
      use Symfony'Bundle'FrameworkBundle'Templating'EngineInterface;
      use Symfony'Component'Routing'RouterInterface;
      use FOS'UserBundle'Model'UserInterface;
      use MonApp'UserBundle'Mailer'MailerInterface;
      class Mailer implements MailerInterface
      {
          protected $mailerMonApp;
          protected $router;
          protected $templating;
          protected $parameters;
          public function __construct($mailerMonApp, RouterInterface $router, EngineInterface $templating, array $parameters) {
               $this->mailerMonApp = $mailerMonApp;
               $this->router = $router;
               $this->templating = $templating;
               $this->parameters = $parameters;
          }
          public function sendConfirmationEmailMessage(UserInterface $user) {
               The function...
          }
          public function sendResettingEmailMessage(UserInterface $user) {
               The function...
          }
          protected function sendEmailMessage($renderedTemplate, $fromEmail, $toEmail) {
              $renderedLines = explode("'n", trim($renderedTemplate));
              $subject = $renderedLines[0];
              $body = implode("'n", array_slice($renderedLines, 1));
              mail($toEmail, $subject, $body, 'noreply@nondedomaine.fr');
          }
     }

我添加了我的类作为服务:

services:
     monapp.mailer:
          class: MonApp'UserBundle'Mailer'Mailer

并更改了config.yml:

service:
    mailer: monapp.mailer

这里是开发环境中的ERROR

     Warning: Missing argument 1 for MonApp'UserBundle'Mailer'Mailer::__construct(), called in /home/domaine/www/nomappli/app/cache/dev/appDevDebugProjectContainer.php on line 831 and defined in /home/domaine/www/nomappli/src/MonApp/UserBundle/Mailer/Mailer.php line 29

我的问题是:当我到达重置密码页面时,我写下了我的伪密码,在验证表单后,我得到了一个白色页面。我想我调用了教程中使用的class and use,所以我不理解我的问题,也许我忘记了使用?

感谢

编辑这里的日志错误(感谢迈克尔和扬帮助我):

[2015-03-31 14:55:35] request.INFO: Matched route "fos_user_resetting_send_email" (parameters: "_controller": "FOS'UserBundle'Controller'ResettingController::sendEmailAction", "_route": "fos_user_resetting_send_email") [] []
[2015-03-31 14:55:35] security.INFO: Populated SecurityContext with an anonymous Token [] []
[2015-03-31 14:55:35] request.CRITICAL: Uncaught PHP Exception InvalidArgumentException: "Unable to find template ""." at /home/nomappli/www/monapp/vendor/symfony/symfony/src/Symfony/Bridge/Twig/TwigEngine.php line 128 {"exception":"[object] (InvalidArgumentException: Unable to find template '"'". at /home/nomappli/www/monapp/vendor/symfony/symfony/src/Symfony/Bridge/Twig/TwigEngine.php:128, Twig_Error_Loader: Unable to find template '"'". at /home/nomappli/www/monapp/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php:106, InvalidArgumentException: Template name '"'" is not valid (format is '"bundle:section:template.format.engine'"). at /home/nomappli/www/monapp/app/cache/prod/classes.php:742)"} []
[2015-03-31 14:55:35] security.DEBUG: Write SecurityContext in the session [] []

您不需要复制粘贴接口代码。它应该在它的位置:在FOSUserBundle中。您只需要实现这个接口。这意味着您需要创建用于放置implements MailerInterface的类。

你已经上过这门课了。只需将use MonApp'UserBundle'Mailer'MailerInterface;替换为use FOS'UserBundle'Mailer'MailerInterface,并删除您的原始界面副本。

更新

当你更新你的问题时,我已经更新了我的答案。您当前的问题与服务配置有关。您需要在服务定义中为构造函数注入参数:

services:
     monapp.mailer:
          class: MonApp'UserBundle'Mailer'Mailer
          arguments:
              - @router
              - @templating
              - {from_email: {confirmation: %fos_user.registration.confirmation.from_email%, resetiing: %fos_user.resetting.email.from_email%}}

同时修改您的构造函数方法:从参数中删除$mailerMonApp

      public function __construct(RouterInterface $router, EngineInterface $templating, array $parameters) {
           $this->router = $router;
           $this->templating = $templating;
           $this->parameters = $parameters;
      }

事实上,问题很明显,您缺少了对服务的一些引用。您的类需要(作为构造函数args)一些参数才能工作。但是您的服务定义没有提供任何这些。我认为您需要阅读一些关于Symfony服务的文档。