0

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):

1

2 Answers 2

2

I was having problems with $transportBuilder on __construct() and I removed all var/generation/* and compiled again and worked.

rm -rf [MAGENTO_PATH]/var/generation/*

2
  • yes sometimes it picks data from cache so you need to clear cache to see your changes.
    – Gulshan
    Mar 21, 2017 at 8:39
  • @Gulshan Yes! You're right. But fortunately, it worked. Thank you! Mar 22, 2017 at 4:03
1

I have just followed the first link you shared. Please check their construct:

public function __construct( \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder){
    $this->_transportBuilder = $transportBuilder;
}

You haven't created transbuilder instance so it was throwing error. Please check and revert.

2
  • I did it! I found the problem.. The is with another thing... Thank you! Mar 21, 2017 at 1:45
  • Welcome and happy coding. :)
    – Gulshan
    Mar 21, 2017 at 1:46

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.