I'm trying to send emails using observer triggers, but I'm having problems with _transportBuilder
(I can't get TransportBuilder
instance).
I'm catching customer_save_after_data_object
event and calling my custom class (it's working), but when I try to execute $transport = $this->_transportBuilder...
I found issues.
public function execute(\Magento\Framework\Event\Observer $observer)
{
$vars = array();
$transport = $this->_transportBuilder
->setTemplateIdentifier(1) // My email template
->setTemplateOptions( [
'area' => \Magento\Framework\App\Area::AREA_FRONTEND, // this is using frontend area to get the template file if admin then \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
])
->setTemplateVars($vars)
->setFrom('[email protected]', 'Test')
->addTo('[email protected]', 'Somepeople')
->getTransport();
$transport->sendMessage();
}
Result:
Fatal error: Uncaught Error: Call to a member function setTemplateIdentifier() on null in /opt/bitnami/apps/magento/htdocs/app/code/Company/mail/Observer/SendMail.php:62 Stack trace:
#0 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/Event/Invoker/InvokerDefault.php(73): Company\mail\Observer\SendMail->execute(Object(Magento\Framework\Event\Observer))
#1 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/Event/Invoker/InvokerDefault.php(61): Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod(Object(Company\mail\Observer\SendMail), Object(Magento\Framework\Event\Observer))
#2 /opt/bitnami/apps/magento/htdocs/vendor/magento/framework/Event/Manager.php(66): Magento\Framework\Event\Invoker\InvokerDefault->dispatch(Array, Object(Magento\Framework\Event\Observer))
#3 /opt/bitnami/apps/magento/htdocs/var/generation/Magento/Framework/Event/Manager/Proxy.php(95): Magento\Framework\Event\Manager->dispatch('customer_save_a...', Array)
#4 /opt/bitnami/apps/magento/htdocs/vendor/magento/mo in /opt/bitnami/apps/magento/htdocs/app/code/Company/mail/Observer/SendMail.php on line 62
Comments:
I've tried to start setting "TemplateOptions" or "TemplateVars" and I've got the same problem. I believe the error is with the instance not found.
Links (Questions followed):