2

I have a project in symfony using FosUserBundle and PugxMultiUserBundle because I need 2 user types. There are CmsUsers and Platform players.

I need a way to have Fos email templates (resetting template,registering template etc.) per user type. One reset template for CmsUser and another template for Players. Same thing for Registering.

The problem occurs because these templates are configured in config.yaml

fos_user:
    db_driver:       orm
    firewall_name:   api
    user_class:      PanelBundle\Entity\User
    from_email:
        address:     '%fos_from_address%'
        sender_name: '%fos_from_name%'
    service:
        mailer:      api.custom_mailer
        user_manager: pugx_user_manager
    registration:
        confirmation:
            enabled:     true
            template:    'ApiBundle:Email:confirm.email.twig'
    resetting:
        retry_ttl: 1800 # After how much seconds (30 min) user can request again pass reset
        token_ttl: 604800 # After how much seconds (1 week) user token is valid (inactive in user mailbox)
        email:
            template:    'ApiBundle:Email:resetting.email.twig'

I need a way to config or implement this in conditional way. If the user type is CmsUser load this template, else load another.

<?php

namespace ApiBundle\Mailer;

use FOS\UserBundle\Mailer\TwigSwiftMailer as BaseMailer;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class CustomUserMailer extends BaseMailer
{
    public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters)
    {
        parent::__construct($mailer, $router, $twig, $parameters);
    }

    /**
     * @param string $templateName
     * @param array  $context
     * @param string $fromEmail
     * @param string $toEmail
     */
    protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
    {
        // Create a new mail message.
        $message = \Swift_Message::newInstance();

        $context['images']['top']['src'] = $message->embed(\Swift_Image::fromPath(
            __DIR__.'/../../../web/assets/img/email/header.jpg'

        ));
        $context['images']['bottom']['src'] = $message->embed(\Swift_Image::fromPath(
            __DIR__.'/../../../web/assets/img/email/footer.jpg'
        ));

        $context = $this->twig->mergeGlobals($context);
        $template = $this->twig->loadTemplate($templateName);
        $subject = $template->renderBlock('subject', $context);
        $textBody = $template->renderBlock('body_text', $context);
        $htmlBody = $template->renderBlock('body_html', $context);

        $message->setSubject($subject);
        $message->setFrom($fromEmail);
        $message->setTo($toEmail);
        $message->setBody($htmlBody, 'text/html');
        $message->addPart($textBody.'text/plain');

        $this->mailer->send($message);
    }
}
billy tzez
  • 155
  • 1
  • 2
  • 11

1 Answers1

0

Hy,

if you can desactivate the FosUserBundle sending emails and create a custom listener who made the same things, get the code of the FosUserBundle listeners and adapt it (ex: add a if($user instanceof CmsUser) etc.) to send custom emails per type.

Check the listeners (EmailConfirmationListener and ResettingListener) in /vendor/friendsofsymfony/user-bundle/EventListener

Pec
  • 106
  • 6
  • No , i cant do that. I have extended FosControllers (Reseting or Registering) and there i have conditional code for checking the users type. But this implementations needs much coding and is very complex. – billy tzez Mar 29 '18 at 13:21
  • In your api.custom_mailer you've got public function sendConfirmationEmailMessage(UserInterface $user) and public function sendResettingEmailMessage(UserInterface $user)? if yes you can do that inside. Check this for exemple https://github.com/simplethings/ZetaWebmailBundle/blob/master/UserBundle/ZetaMailer.php and create parameters for templates per type with a switch to set template – Pec Mar 29 '18 at 13:51
  • Yes i have a custom mailer service where i extend the TwigSwiftMailer. – billy tzez Mar 29 '18 at 14:01
  • the problem is , in there i dont have the user . So i cant know for which user type this is called – billy tzez Mar 29 '18 at 14:02
  • i added above the custom mailer which i have implemented – billy tzez Mar 29 '18 at 14:04