Symfony2和FOSUserBundle:覆盖邮件程序

时间:2015-03-31 10:11:59

标签: php symfony service fosuserbundle mailer

我来这里是因为我的OVH和我的symfony2应用程序有问题,我无法使用SwiftMailer发送邮件。 FOSUserBundle在登记后重置密码或激活我的帐户是相当有问题的。所以我想使用PHP中实现的MAIL函数,我遵循文档here。所以我创建了我的班级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

这里是Dev env中的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所以我不明白我的问题,也许我忘了使用它?

由于

修改 这里的日志错误(感谢Michael&amp; Yann帮助我):

[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 [] []

2 个答案:

答案 0 :(得分:2)

您无需复制粘贴界面代码。它应该在它的位置:在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;
      }

答案 1 :(得分:0)

事实上,问题非常清楚,您缺少对服务的一些引用。 您的类需要(作为构造函数args)一些参数才能工作。但是您的服务定义不提供任何这些。 我认为您需要阅读一点documentation about services in Symfony