Symfony mailer - unable to override default mailer -

18 Views Asked by At

I am using symfony sontaa, with sonata project user bundle with fos user bundle too, I am new to sonata and dont have much experience, Any help is appreciated. Thank You

I am facing below error:

Argument 3 passed to Sonata\UserBundle\Mailer\Mailer::__construct() must be an instance of Swift_Mailer, instance of Symfony\Component\Mailer\Mailer given, called in /home/madmachine/projects/tmh/var/cache/dev/ContainerJLMEZzW/srcApp_KernelDevDebugContainer.php on line 2259

In my .env

###> symfony/sendgrid-mailer ###
MAILER_DSN=sendmail://default

In mailer.yaml config file:

framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'

in my services.yaml file:

app.admin.user.mailer:
        class: App\Mailer\AdminMailer
        arguments:  ['@router','@twig','@mailer','%fos_user.resetting.email.from_email%','%fos_user.resetting.email.template%']

in my fos_user.yaml file

fos_user:
    db_driver: orm # other valid values are 'mongodb' and 'couchdb'
    firewall_name: main
    user_class: App\Entity\User

    group:
       group_class:   App\Entity\Group
       group_manager: sonata.user.orm.group_manager # If you're using doctrine orm (use sonata.user.mongodb.group_manager for mongodb)

    from_email:
      address: '%sender_mail%'
      sender_name: '%sender_name%'
    profile:
      form:
        type:               App\Form\UserProfileType
    change_password:
      form:
        validation_groups:  [ChangePassword,AppChangePassword, Default]
    resetting:
      retry_ttl: 7200 # Value in seconds, logic will use as hours
      token_ttl: 833600
      email:
        from_email: # Use this node only if you don't want the global email address for the resetting email
          address:       '%sender_mail%'
          sender_name:    '%sender_name%'
        template:   'front/mailforget.html.twig'
    service:
      mailer: app.admin.user.mailer

This is my AdminMailer class:

<?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) Thomas Rabaix <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Mailer;

use App\Services\sendEMail;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;

final class AdminMailer implements MailerInterface
{
    /**
     * @var UrlGeneratorInterface
     */
    private $urlGenerator;

    /**
     * @var Environment
     */
    private $twig;

    /**
     * @var \Swift_Mailer
     */
    private $mailer;

    /**
     * @var array
     */
    private $fromEmail;

    /**
     * @var string
     */
    private $emailTemplate;

    public function __construct(UrlGeneratorInterface $urlGenerator, Environment $twig,  $mailer, array $fromEmail, string $emailTemplate,sendEMail $sendEMail)
    {
        $this->urlGenerator = $urlGenerator;
        $this->twig = $twig;
        $this->mailer = $mailer;
        $this->fromEmail = $fromEmail;
        $this->emailTemplate = $emailTemplate;
        $this->sendEMail = $sendEMail;
    }

    public function sendResettingEmailMessage(UserInterface $user): void
    {
        $url = $this->urlGenerator->generate('sonata_user_admin_resetting_reset', [
            'token' => $user->getConfirmationToken(),
        ], UrlGeneratorInterface::ABSOLUTE_URL);

        $rendered = $this->twig->render($this->emailTemplate, [
            'user' => $user,
            'confirmationUrl' => $url,
        ]);

        $this->sendMessage($this->emailTemplate,[
            'user' => $user,
            'confirmationUrl' => $url,
        ] ,$this->fromEmail ,(string) $user->getEmail());

    }

    /**
     * @param string $templateName
     * @param array  $context
     * @param array  $fromEmail
     * @param string $toEmail
     */
    protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
    {

        $template = $this->twig->load($templateName);
        $subject = $template->renderBlock('subject', $context);
        $textBody = $template->renderBlock('body_text', $context);

        return $this->sendEMail->sendMail($subject,$toEmail,$templateName,$context);

    }

    public function sendConfirmationEmailMessage(UserInterface $user): void
    {
        throw new \LogicException('This method is not implemented.');
    }
}

the command app.admin.user.mailer resolve to right service

0

There are 0 best solutions below