0

I have create the observer when the customer is register to get notification to admin

 <event name="customer_register_success">
    <observer name="sendmail_toadmin" instance="Namespace\Module \Observer\SendMailToAdmin"/>
</event>

Observer path is app/code/Namespace/Module/Observer/SendMailToAdmin

namespace Namespace\Module\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('1') // 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) 
    {
        \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug($e->getMessage());
    }

}
}

I got the error

we cant save the customer

please help me

  • Are you sure the observer is firing ? @Magento – Prathap Gunasekaran Aug 27 at 6:31
  • When i click on create account it display message We can't save the customer..when i remove the observer then it work properly – Magento Aug 27 at 6:36
  • setTemplateIdentifier('1') provide a valid identifier – Prathap Gunasekaran Aug 27 at 7:04
2

This is just a guess, but maybe the problem is the space in your events.xml file?

It should be: Namespace\Module\Observer\SendMailToAdmin

instead of: Namespace\Module \Observer\SendMailToAdmin

<event name="customer_register_success">
    <observer name="sendmail_toadmin" instance="Namespace\Module\Observer\SendMailToAdmin"/> 
</event>

At first, I think it's just a typo when you write down your question, but your code work fine for me until I also copy that space into my code, then I can replicate the very same error message you described.

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.