Overriding Mail service

In an earlier version of concreteCMS, I did a successful override of the mail service by creating a custom class based on Concrete\Core\Mail\Service. I registered this with a version of MailServiceProvider containing

       $register = [
            'helper/mail' => '\Tops\concrete5\services\TopsMailService',
            'mail' => '\Tops\concrete5\services\TopsMailService',
        ];

TopsMailService simply inserted some code in the SendMail() method.
It worked fine and still works under newer ConcreteCMS versions.
However, wanting to play nice with core, I rewrote it based on the new latest version of the Mail\Service class.

The biggest difference I could see was that the new version has use statements for Laminas\Mail classes where the old version has Zend\Mail references. If I replace these use statements, I get the exception

Illuminate \ Contracts \ Container \ BindingResolutionException
Target [Laminas\Mail\Transport\TransportInterface] is not instantiable while building [\Tops\concrete5\services\TopsMailService].

Huh?

My old custom class still works perfectly, but I am concerned about future compatibility and well, just good practices.

Can anyone suggest a better way to do the override or any modification I could make to avoid the exception. I’ll upload code if anyone is interested.

Another thought, Since I am trying to override the actual method of sending the message (not the message construction) maybe I should try to override the transport class, rather than the service. Not sure how to do that. Any thoughts would be welcome.

I have resolved the problems of the dependency on Lamina or Zend for now. I am still wondering if overriding the transport would be a better approach and would appreciate comments.

It’s better to show your Concrete version

Thanks hissy, and sorry I forgot to indicate my environment details. ConcreteCMS 9.3.9. PhP 8.2. Database 10.6-MariaDB. As I mentioned in the reply, I have resolved the Lamina/Zend dependency issue and no longer get any exceptions. My urgency is passed, but I’m still interested in any opinions and suggestions of better practice to override mail sending.

Here’s the latest on this. All is working fine now except I get a creepy feeling that my solution is not the best practice. Any advice is welcome. Here are more details.

I overrode the mail provider class in application\config\app.php:

return [
    'providers' => array(
       'core_mail' => '\Application\providers\TopsMailServiceProvider'
    )];

In the provider I registered the mail service:

        $app = $this->app;
        $register = [
            'helper/mail' => '\Tops\concrete5\services\TopsMailService',
            'mail' => '\Tops\concrete5\services\TopsMailService',
        ];

The exception I mentioned earlier occurs if there is any reference to Laminas classes. Since I am using my own mailing service I removed all references to Laminas in both classes and all works fine, at least for my purposes.

What I wonder is:

  1. Should I use a different method or location to register and instantiate the mail service? I guess that the reason for the exception was that Laminas and my service were instantiated in the wrong order.
  2. Would it have been a better approach to try to override the transport class?
  3. Any other ideas?

Thanks to all.