yii2-usuario icon indicating copy to clipboard operation
yii2-usuario copied to clipboard

Allow override services classes

Open Slayvin opened this issue 7 years ago • 4 comments

I'd like to override MailService class in order to use an external mailer service (like mailchimp, mailjet...) Basically I need to add specific headers when doing $mailer->compose(), and I can't figure out how to do this with events.

I configured the module like this:

'modules' => [
    'user' => [
        'class' => Da\User\Module::class,
        'classMap' => [
            'User' => common\models\User::class,
            'Profile' => common\models\Profile::class,
            // Forms
            'RegistrationForm' => common\models\RegistrationForm::class,
            // Services
            'MailService' => \common\models\services\MailService::class,

But then I receive this error because 'MailService' is not part of the $routes in the Bootstrap class: Unknown configuration class name 'MailService'.

So, for now, I'm overriding the Bootstrap class, so I can override the Mailservice class...

Is it a good practice, or is there a better way to achieve what I want to ?

Slayvin avatar Jun 20 '18 13:06 Slayvin

MailService isn't listed in the current overridable classes.

If the modifications is so trivial you can make a pull request with your patch. If you can, please don't forget to update documentation and CHANGELOG with your change! thanks

maxxer avatar Jun 20 '18 15:06 maxxer

@Slayvin MailService is using the Yii's application mail interface https://github.com/2amigos/yii2-usuario/blob/master/src/User/Service/MailService.php#L17

That is the mailer configured by Yii. You should simply use the SMTP details of your desired service and that's it. I do not see any reason to override the MailService

tonydspaniard avatar Jul 22 '18 23:07 tonydspaniard

I know it's using the application mail interface, but I needed to do this:

return $this->mailer
    ->compose()
    ->setFrom($this->from)
    ->setTo($this->to)
    ->addHeader('X-MJ-TemplateID', $template) // swiftmailer-message
    ->addHeader('X-MJ-Vars', $json_vars)
    ->send();

So I can't just edit the general SMTP config, since I need to inject variables into some headers.

See https://www.yiiframework.com/extension/yiisoft/yii2-swiftmailer/doc/api/2.2/yii-swiftmailer-message for the addHeader method

Slayvin avatar Jul 30 '18 12:07 Slayvin

@Slayvin I think there is a bigger issue as the MailService is heavily used by MailFactory class, which is where the actual MailService is created.

 return Yii::$container->get(
            MailService::class,
            [$type, $from, $to, $subject, $view, $params, Yii::$app->getMailer()]
        );

As you can see above, we are using Yii::$container, therefore, I can think of two ways:

  1. Register a class definition to your MailService (it will have to play with the same arguments used on the MailFactory class or it will fail (i.e. $view = $template, $params['json_vars'])
  2. Create your very own Module Bootstrap class and add whatever configuration you wish.

Note: Never tested nor tried the above

tonydspaniard avatar Jun 29 '19 11:06 tonydspaniard