1

I'm creating an extension which sends email from command console and / or cron.

https://github.com/DominicWatts/BackInStock/tree/develop

In https://github.com/DominicWatts/BackInStock/blob/develop/Helper/Data.php#L168-L226

I've got this

const XML_PATH_EMAIL_TEMPLATE = 'backinstock/email/template';

    public function sendTransactionalEmail($vars = [])
    {
        $email = $vars['email'] ?? null;
        if (empty($vars) || !$email) {
            return;
        }
        $this->inlineTranslation->suspend();
        try {
            $postObject = new \Magento\Framework\DataObject();
            $postObject->setData($vars);

            $recipient = [
                'firstname' => $this->escaper->escapeHtml($vars['firstname']),
                'email' => $this->escaper->escapeHtml($vars['email']),
            ];

            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;

            $this->transportBuilder->setTemplateIdentifier(
                $this->scopeConfig->getValue(
                    self::XML_PATH_EMAIL_TEMPLATE,
                    $storeScope
                )
            )->setTemplateOptions(
                [
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                ]
            )->setTemplateVars(
                [
                    'firstname' => $vars['firstname'] ?? null,
                    'lastname' => $vars['lastname'] ?? null,
                    'email' => $email,
                    'productname' => $vars['productname'] ?? null,
                    'productlink' => $vars['productlink'] ?? null
                ]
            )->setFrom(
                $this->scopeConfig->getValue(
                    self::XML_PATH_EMAIL_IDENTITY,
                    $storeScope
                )
            )->addTo(
                $recipient['email'],
                $recipient['firstname']
            );

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

            return true;
        } catch (Exception $e) {
            $this->logger->critical($e);
        }
        return false;
    }

https://github.com/DominicWatts/BackInStock/blob/develop/etc/email_templates.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="backinstock_email_template" label="Back In Stock" file="back_in_stock.html" type="html" module="Xigne_BackInStock" area="frontend"/>
</config>

Email template is in place

https://github.com/DominicWatts/BackInStock/blob/develop/view/frontend/email/back_in_stock.html

Then with some dummy data in I try and run

php bin/magento xigen:backinstock:check check

2019-07-08 23:15:04 Start Notification Process


  [UnexpectedValueException]                        
  Template file 'back_in_stock.html' is not found.  


xigen:backinstock:check <check>

So that says to me my XML and logic is correct.

Magento is just not seeing my email template. Trouble is I just can't see what's wrong. Path looks correct.

Tried clearing cache, di compile, setup upgrade.

3 Answers 3

4

The issue was a typo in email_templates.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="backinstock_email_template" label="Back In Stock" file="back_in_stock.html" type="html" module="Xigen_BackInStock" area="frontend"/>
</config>

module="Xigen_BackInStock" part was incorrect. So all values being pulled through were correct and files were in the right place. That was enough to make magento not look in the right place.

1

Change following code:

'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,

DEFAULT_STORE_ID is 0 which is for admin. Assing valid store id instead of 0. Ex:

'store' => 1

OR

Transfer your template to view/base/email folder.

2
1

You can try this way,

Get Store id like this, add this class in your __construct()

Magento\Store\Model\StoreManagerInterface

'store' => $this->storeManager->getStore()->getId());

$transport = $this->transportBuilder->setTemplateIdentifier(
                $this->scopeConfig->getValue(
                    self::XML_PATH_EMAIL_TEMPLATE,
                    $storeScope
                )
            )->setTemplateOptions(
                [
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => $this->storeManager->getStore()->getId());
                ]
            )->setTemplateVars(
                [
                    'firstname' => $vars['firstname'] ?? null,
                    'lastname' => $vars['lastname'] ?? null,
                    'email' => $email,
                    'productname' => $vars['productname'] ?? null,
                    'productlink' => $vars['productlink'] ?? null
                ]
            )->setFrom(
                $this->scopeConfig->getValue(
                    self::XML_PATH_EMAIL_IDENTITY,
                    $storeScope
                )
            )->addTo(
                $recipient['email'],
                $recipient['firstname']
            )->getTransport();

            $transport->sendMessage();
4

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.