0

Magento 2 send email(adminhtml) programatically not working. i am using this code -

$report = [
                'report_date' => date("j F Y", strtotime('-1 day')),
                'orders_count' => rand(1, 10),
                'order_items_count' => rand(1, 10),
                'avg_items' => rand(1, 10)
            ];

            $postObject = new \Magento\Framework\DataObject();
            $postObject->setData($report);

            $transport = $this->_transportBuilder
                ->setTemplateIdentifier('sendcredentials')
                ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID])
                ->setTemplateVars(['data' => $postObject])
                ->setFrom(['name' => 'Robot','email' => '[email protected]'])
                ->addTo(['[email protected]', '[email protected]'])
                ->getTransport();
            $transport->sendMessage();

and email_templates.xml(location-etc)

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:Magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="sendcredentials" label="Test email" file="sendcredentials.html" type="html" module="Vendor_Sendcredentials" area="adminhtml"/>
</config>

and HTML file sendcredentials.html(location-view/adminhtml/email) -

<!--@subject Custom @-->
<h3>Report from {{var data.report_date|raw}}</h3>

<p><b>Number of orders:</b> {{var data.orders_count|raw}}</p>
<p><b>Items count:</b> {{var data.order_items_count|raw}}</p>
<p><b>Average items per order:</b> {{var data.avg_items|raw}}</p>

I am getting this error -

Template file 'sendcredentials.html' is not found.

3
  • Which path you have the sendcredentials.html file? Commented Apr 3, 2019 at 14:37
  • It suppose to be in view/adminhtml/email/ folder, Please verify it Commented Apr 3, 2019 at 14:38
  • yes it is there,i commented my question Commented Apr 3, 2019 at 14:39

1 Answer 1

1

I solved my self,changed area

from

'area' => \Magento\Framework\App\Area::AREA_FRONTEND,

to

'area' => \Magento\Framework\App\Area::AREA_ADMINHTML,

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.