I need to collect SubScriber email in Magento 2. So, I tried to inject \Magento\Newsletter\Model\Subscriber $subscriber
, I didn't work for me.
<?php
namespace Vendor\Module\Cron;
class NewsLetter
{
protected $_logger;
protected $_subscriber;
protected $_customerdata;
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Newsletter\Model\Subscriber $subscriber,
\Magento\Customer\CustomerData\Customer $customerdata,
)
{
$this->_logger = $logger;
$this->_subscriber= $subscriber;
$this->_customerdata= $customerdata;
}
public function execute()
{
$customer= $this->_customerdata->getSectionData();
$data=$this->getCustomerEmail($customer)
foreach($data as $email)
{
$customerEmail =$email;
$checkSubscriber = $this->_subscriber->loadByEmail($customerEmail);
if ($checkSubscriber->isSubscribed()) {
// Customer is subscribed
$this->_logger->addDebug($customerEmail."subscribed Email");
} else {
$this->_logger->addDebug($customerEmail."Unsubscribed Email");
}
}
}
}
Suggest me what I missed.