2
votes

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

1 Answers

0
votes

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