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' => 'robot@server.com'])
                ->addTo(['test@gmail.com', 'test@test.in'])
                ->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.

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

I solved my self,changed area

from

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

to

'area' => \Magento\Framework\App\Area::AREA_ADMINHTML,
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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