Magento 2 – How to Send Confirmation Mail for Newsletter

magento2

I am using Magento 2.1.5, and I enabled confirm function in admin.

Need to Confirm – yes

Now when anyone subscribes, they get confirmation link, but after click on that link,they successfully verify, but they are not getting again success email of subscribe. However, status is changed correctly in admin panel.

This is confirm.php file:

  public function execute()
{
    $id = (int)$this->getRequest()->getParam('id');
    $code = (string)$this->getRequest()->getParam('code');

    if ($id && $code) {
        /** @var \Magento\Newsletter\Model\Subscriber $subscriber */
        $subscriber = $this->_subscriberFactory->create()->load($id);

        if ($subscriber->getId() && $subscriber->getCode()) {
            if ($subscriber->confirm($code)) {
                $this->messageManager->addSuccess(__('Your subscription has been confirmed.'));
            } else {
                $this->messageManager->addError(__('This is an invalid subscription confirmation code.'));
            }
        } else {
            $this->messageManager->addError(__('This is an invalid subscription ID.'));
        }
    }

    $this->getResponse()->setRedirect($this->_storeManager->getStore()->getBaseUrl());
}

}

here the code only for getting message after verify, but here it is not sending mail after verification. What should I change here to send success email after verification email?

Best Answer

Let me answer of my question :

Search in the file /vendor/magento/module/Newsletter/Model/Subscriber.php for

public function confirm($code)
{
    if($this->getCode()==$code) {
        $this->setStatus(self::STATUS_SUBSCRIBED)
            ->setIsStatusChanged(true)
            ->save();
        return true;
    }
    return false;
}

** * * ** And replace it with:

public function confirm($code)
{
    if($this->getCode()==$code) {
        $this->setStatus(self::STATUS_SUBSCRIBED)
            ->setIsStatusChanged(true)
            ->save();

            $this->sendConfirmationSuccessEmail();
        return true;
    }

    return false;
}

Note: instead of change in default file, overwrite this in your custom module.