2

I am trying to intercept outgoing emails from joomla and reroute them to my email address for development and testing. Ideally I could set up a toggle under Global Configuration > Server > Email settings so that other non-developer teammates can control it easily as well, but the back end needs to be in place first.

If there is a plugin or built in that handles this kind of thing, that would be great but I can't find it.

I have read through and attempted the following (very old) solutions but I don't think it is going to work with 3.9. At least not without some tweaking:

I found a newer override solution, but I'm not sure it will work with core joomla email functions:

1
  • Welcome to JSE, Joseph. This is a well-crafted question with proof of research -- good job. Please take our tour and consider telling us a bit about your Joomla journey by editing your profile. Please continue to work on this challenge while you wait for assistance. If you manage to self-solve, you are encouraged to post an educational answer to your own question. Hopefully our membership will be able to offer some guidance in the very near future. Jan 30, 2021 at 3:54

2 Answers 2

1

You could use this extension: https://extensions.joomla.org/extension/jmail-queue/

And then simply view the emails in the queue. Or add the onInjection plugin to remove all the recipients and substitute the one you want.

1
  • Congratulations on making your first post and thank you for contributing to the knowledge pool. Please take our tour. Feb 5, 2021 at 20:34
1

I'm not sure how reliable this is but it can be done by setting Joomla\CMS\Factory::$mailer to an instance of your custom mailer. This should be done by a system plugin early in the request. The class should extend Joomla\CMS\Mail\Mail and the instance should already be configured to use settings from global configuration.

defined('_JEXEC') or exit;

use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;

class PlgSystemCustommail extends CMSPlugin
{
    public function onAfterInitialise()
    {
        // This check isn't necessary but it ensures mail is sent using only one mailer, whichever it may be. Add custom logic as needed.
        if (Factory::$mailer === null)
        {
            // Set the mailer.
            Factory::$mailer = $this->createMailer();
        }
    }
    
    private function createMailer(): MyMail
    {
        // Load the custom class.
        JLoader::register('MyMail', __DIR__ . '/mymail.php');

        // Instantiate the custom mailer.
        $mailer = new MyMail;
        
        // Configure the mailer here. See `Joomla\CMS\Factory::createMailer()` for an example.
        
        // Return the mailer instance.
        return $mailer;
    }
}

Custom mailer class modified to send mail to specific address:

defined('_JEXEC') or exit;

use Joomla\CMS\Mail\Mail;

class MyMail extends Mail
{
    public function send()
    {
        // Clear recipient list.
        $this->clearAddresses();
        
        // Add custom recipient.
        $this->addRecipient($address, $name);
        
        return parent::send();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.