1

I'm trying to set up an observer that sends an email to the admin when a user registers on the website. I am following a tutorial but have hit a dead end. The observer is working correctly however I am getting this error in the browser:

Type Error occurred when creating object: Ubr\NotifyEmail\Observer\SendMailToAdmin

And this error in system.log:

[2019-03-12 07:21:11] main.CRITICAL: Type Error occurred when creating object: Ubr\NotifyEmail\Observer\SendMailToAdmin, Argument 1 passed to Ubr\NotifyEmail\Observer\SendMailToAdmin::__construct() must be an instance of Magento\Framework\Mail\Template\TransportBuilder, instance of Magento\Customer\Model\ResourceModel\CustomerRepository\Interceptor given, called in /home/ubuildro/public_html/mag/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 116 [] []

Here is my events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_register_success">
        <observer name="sendmail_toadmin" instance="Ubr\NotifyEmail\Observer\SendMailToAdmin"/>
    </event>
</config>

And my SendMailToAdmin.php

<?php

namespace Ubr\NotifyEmail\Observer;

use Magento\Framework\Event\ObserverInterface;

class SendMailToAdmin implements ObserverInterface
{

    const XML_PATH_EMAIL_RECIPIENT = 'trans_email/ident_general/email';
    protected $_transportBuilder;
    protected $inlineTranslation;
    protected $scopeConfig;
    protected $storeManager;
    protected $_escaper;

    public function __construct(
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Escaper $escaper
    ) {
        $this->_transportBuilder = $transportBuilder;
        $this->inlineTranslation = $inlineTranslation;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $this->_escaper = $escaper;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {


        $customer = $observer->getData('customer');

        $this->inlineTranslation->suspend();
        try 
        {
            $error = false;

            $sender = [
                'name' => $this->_escaper->escapeHtml($customer->getFirstName()),
                'email' => $this->_escaper->escapeHtml($customer->getEmail()),
            ];
            $postObject = new \Magento\Framework\DataObject();
            $postObject->setData($sender);
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; 
            $transport = 
                $this->_transportBuilder
                ->setTemplateIdentifier('3') // Send the ID of Email template which is created in Admin panel
                ->setTemplateOptions(
                    ['area' => \Magento\Framework\App\Area::AREA_FRONTEND, // using frontend area to get the template file
                    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,]
                )
                ->setTemplateVars(['data' => $postObject])
                ->setFrom($sender)
                ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
                ->getTransport();
            $transport->sendMessage(); ;
            $this->inlineTranslation->resume();


        } 
        catch (\Exception $e) 
        {

        }

    }

}

Like I said the observer is working correctly so the error is occuring in SendMailToAdmin.php please help.

Your Answer

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

Browse other questions tagged or ask your own question.