0

I want to send email id of newly subscriber user to admin mail, below is my module code .

mail is working fine when new subscriber is added but I am unable to get subscriber email id helper/data.php

app/code/Stack/NewsletterNotify/etc/email_templates.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="newsletter_subscription_success_admin_email_template" label="Subscription Notification to Admin" file="subscr_notify_admin.html" type="html" module="Stack_NewsletterNotify" area="frontend"/>
</config>

app/code/Stack/NewsletterNotify/etc/frontend/events.xml and the following content in it:

<?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="newsletter_subscriber_save_after">
        <observer name="newsletter_subscriber_save_after_event" instance="Stack\NewsletterNotify\Observer\NotifyAdmin"/>
    </event>
</config>

file app/code/Stack/NewsletterNotify/Observer/NotifyAdmin.php with the following content:

<?php
namespace Stack\NewsletterNotify\Observer;

use Magento\Framework\Event\Observer;

class NotifyAdmin implements \Magento\Framework\Event\ObserverInterface {
    private $logger;
    private $helper;

    public function __construct(\Stack\NewsletterNotify\Helper\Data $helper, \Psr\Log\LoggerInterface $logger) {
        $this->helper = $helper;
        $this->logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $event = $observer->getEvent();
        $subscriberEmail = $event->getSubscriber()->getSubscriberEmail();
        $subscriberStatus = $event->getSubscriber()->getSubscriberStatus();
        if ($subscriberStatus == 1) {
            $this->helper->notifyAdmin($subscriberEmail);
        }
    }

}

file Data.php under app/code/Stack/NewsletterNotify/Helper and put the following content:

<?php

namespace Stack\NewsletterNotify\Helper;

class Data {
    protected $_logger;
    protected $_storeManager;
    private $scopeConfig;
    private $transportBuilder;
    private $inlineTranslation;

    public function __construct(\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Psr\Log\LoggerInterface $logger) {

        $this->inlineTranslation = $inlineTranslation;
        $this->transportBuilder = $transportBuilder;
        $this->scopeConfig = $scopeConfig;
        $this->_storeManager = $storeManager;
        $this->_logger = $logger;
    }

    public function notifyAdmin($email) {
        try {
            $this->inlineTranslation->suspend();

            $this->transportBuilder
                    ->setTemplateIdentifier('newsletter_subscription_success_admin_email_template')
                    ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->_storeManager->getStore()->getId()])
                    ->setTemplateVars(['subscriber' => $this])
                    ->setFrom(array('email' => '{sender email}', 'name' => '{sender name}'))
                    ->addTo({admin email}, {admin name});
            $transport = $this->transportBuilder->getTransport();
            $transport->sendMessage();

            $this->inlineTranslation->resume();
        } catch (\Exception $e) {
            $this->_logger->critical($e->getMessage());
        }

        return $this;
    }

    public function getStoreId()
    {
        return $this->_storeManager->getStore(true)->getId();
    }

}

Content of mailer html

Update: Sample content for subscr_notify_admin.html file

<!--@subject {{trans "Subscription Notification to Admin"}} @-->
<!--@vars {
"template config_path=\"design\/email\/footer_template\"":"Email Footer Template",
"template config_path=\"design\/email\/header_template\"":"Email Header Template"
} @-->


{{template config_path="design/email/header_template"}}

{{trans "A new user has subscribed to our newsletter."}}

{{template config_path="design/email/footer_template"}}
1

In the app/code/Stack/NewsletterNotify/Helper/Data.php file, please replace the function notifyAdmin() as below:

public function notifyAdmin($email) {
    try {
        $to = {admin email};
        $from = ['email' => '{sender email}', 'name' => '{sender name}'];
        $templateOptions = ['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->getStoreId()];
        $templateVars = ['subscriberemail' => $email];
        $this->inlineTranslation->suspend();

        $this->transportBuilder
                    ->setTemplateIdentifier('newsletter_subscription_success_admin_email_template')
                    ->setTemplateOptions($templateOptions)
                    ->setTemplateVars($templateVars)
                    ->setFrom($from)
                    ->addTo($to);
            $transport = $this->transportBuilder->getTransport();
            $transport->sendMessage();

            $this->inlineTranslation->resume();
        } catch (\Exception $e) {
            $this->_logger->critical($e->getMessage());
        }

        return $this;
    }

and then call the template variable subscriberemail in the email template as mentioned below:

{{var subscriberemail}}

Please let me know if you have any other question.

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.