0

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.

2 Answers 2

0

Include this into your code is like this.

use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Area;

........... ...........

$storeScope = ScopeInterface::SCOPE_STORE;
$transport = $this->transportBuilder
           ->setTemplateIdentifier('test_template')
           ->setTemplateOptions([
               'area' => Area::AREA_FRONTEND,
               'store' => YOUR_CURRENT_STORE_ID,
            ])
           ->setTemplateVars($vars)
           ->setFrom($sender)
           ->addTo($toEmail);
5
  • I tried this, but it's not working. Same exception. My example contains static data for area, store, etc., but I also tried with dynamic data as you suggested which is not working.
    – iupaem
    Jan 9 at 9:54
  • confirm this call from frontend of magento, and also store data is pass properly. Jan 9 at 9:57
  • Yes, I can confirm that. Also, the email template is associated with the frontend area. I see you're mentioning @storeScope, but you never use it.
    – iupaem
    Jan 9 at 10:06
  • storeScope need to use get current store information like id. Jan 9 at 11:55
  • I noticed the error is caused by the following line: $transport = $this->transportBuilder->getTransport(); Before that line, the area code is defined, but after it, is not defined anymore.
    – iupaem
    Jan 9 at 13:03
0

we can use it as 'area' => \Magento\Framework\App\Area::AREA_FRONTEND

the below code had worked for me,hope it helps

$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $templateOptions = [
            'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
            'store' => $storeId
        ];
        $transport = $this->transportBuilder->setTemplateIdentifier($templateId, $storeScope)
            ->setTemplateOptions($templateOptions)
            ->setTemplateVars($templateVars)
            ->setFrom($from)
            ->addTo($toEmail)
            ->getTransport();
        $transport->sendMessage();
1
  • I tried this, but it doesn't work. I noticed the error is caused by the following line: $transport = $this->transportBuilder->getTransport(); Before that line, the area code is defined, but after it, is not defined anymore. So, I think the error is related to something else, not the email sending code.
    – iupaem
    Jan 9 at 13:35

Your Answer

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

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