php - Symfony2 和 FOSUserBundle : Override Mailer

标签 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, '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="442a2b362134283d042a2b2a2021202b29252d2a216a2236" rel="noreferrer noopener nofollow">[email protected]</a>');
          }
     }

我将我的类(class)添加为服务:

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

并更改了config.yml:

service:
    mailer: monapp.mailer

这里是开发环境中的错误:

     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

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

谢谢

编辑 这里是日志错误(感谢 Michael 和 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 [] []

最佳答案

您不需要复制粘贴界面代码。它应该在它的位置:FOSUserBundle 中。你只需要实现这个接口(interface)即可。这意味着您需要创建在其中放置 implements MailerInterface 的类。

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

更新

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

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;
      }

关于php - Symfony2 和 FOSUserBundle : Override Mailer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29366068/

相关文章:

php - PDF 文件是否可以定义 0 个页面,否则会导致页面大小为 0?

php - 监听 Symfony 2 中的所有事件

symfony - DoctrineExtensions 软删除

templates - Twig:否定包含运算符 IN

php - 如何检查电子邮件域是否是 php 中的 gmail,然后删除 "@gmail.*"?

php - 单个产品页面中的 WooCommerce added_to_cart 委托(delegate)事件

javascript - jQuery 动态表单,且 PHP $_FILES 为空

windows - 在 Windows 2012 的 LocalSystem 帐户上运行 Internet Explorer

iphone - 如何在 iPhone 上访问 safari、facebook 等应用程序的远程连接请求

php - 如何检测 MySQL 服务(或简称 MySQL)是否可用于 PHP?