0

Trying to follow thuis tutorial: https://magecomp.com/blog/send-email-to-admin-after-customer-registration-magento-2/

What i have done:

  1. \app\code\Namespace\Modulename\etc\module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> 
<module name="Namespace_Modulename" setup_version="0.0.1">
</module> </config>
  1. \app\code\Namespace\Modulename\registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Namespace_Modulename',
__DIR__
);
  1. \app\code\Namespace\Modulename\etc\frontend\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="Namespace\Modulename\Observer\SendMailToAdmin"/>
        </event>
    </config>
  1. \app\code\Namespace\Modulename\Observer\SendMailToAdmin.php
    <?php

    namespace Namespace_Modulename\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('13') // 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());
            }

        }

    }

Error in browser:

Fatal error: Cannot declare class Namespace_Modulename\Observer\SendMailToAdmin, because the name is already in use in /home/qg2ylpmw/public_html/app/code/Namespace/Modulename/Observer/SendMailToAdmin.php on line 7

What's wrong?? What i need to do to get to work?

Magento ver. 2.2.11

1 Answer 1

1

Change this namespace Namespace_Modulename\Observer; to

namespace Namespace\Modulename\Observer;

[Update]

Do you use module name as Namespace_Modulename?

If your answer is yes, then rename it to something that is not Namespace. Ex. AnaMagento_Modulename. Now replace Namespace to AnaMagento.

[Update]

app\code\AnaMagento\Modulename\etc\module.xml

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="AnaMagento_Modulename" setup_version="0.0.1">
    </module>
</config>

app\code\AnaMagento\Modulename\registration.php

<?PHP 
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE, 'AnaMagento_Modulename',
    __DIR__
);

app\code\AnaMagento\Modulename\etc\frontend\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="AnaMagento\Modulename\Observer\SendMailToAdmin"/>
    </event>
</config>

app\code\AnaMagento\Modulename\Observer\SendMailToAdmin.php

<?php

namespace AnaMagento\Modulename\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('13') // 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());
        }

    }

}
7
  • Now the error is: Parse error: syntax error, unexpected 'Namespace' (T_NAMESPACE), expecting '{' in /home/qg2ylpmw/public_html/app/code/Namespace/Modulename/Observer/SendMailToAdmin.php on line 3
    – AnaMagento
    Commented Apr 7, 2020 at 17:27
  • Change module name, check updated answer
    – Sohel Rana
    Commented Apr 7, 2020 at 17:32
  • I'm sorry, it shows again the same error =( Fatal error: Cannot declare class AnaMagento_Modulename\Observer\SendMailToAdmin, because the name is already in use in /home/qg2ylpmw/public_html/app/code/AnaMagento/Modulename/Observer/SendMailToAdmin.php on line 7
    – AnaMagento
    Commented Apr 7, 2020 at 21:57
  • i need to change SendMailToAdmin.php, right? and the same for the other files... because that's what i did...
    – AnaMagento
    Commented Apr 7, 2020 at 22:02
  • Check the updated answer
    – Sohel Rana
    Commented Apr 8, 2020 at 3:22

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.