Skip to content

“Area code is not set” when trying to send emails

I am trying to send emails programmatically on Magento 2.4.4-p5 using the TransportBuilder, but I get an exception: Area code is not set, even if I set it under the setTemplateOptions method.

This is the content of my custom method used to send emails that is added in a helper file.

        $sender = ['email' => '[email protected]', 'name' => 'Domain'];
        $vars = [
            'message' => 'test here'
        ];
        $this->transportBuilder
            ->setTemplateIdentifier('test_template')
            ->setTemplateOptions(['area' => 'frontend', 'store' => 0])
            ->setTemplateVars($vars)
            ->setFrom($sender)
            ->addTo($toEmail);

        $transport = $this->transportBuilder->getTransport();
        $transport->sendMessage();

A workaround would be to set the area as GLOBAL, but then the email template is not to be found.

Is this a known issue?
Am I missing something?

Thank you.