In Magento 2, you’re able to enable/disable email for newsletter confirmation at Store -> Configuration -> Customers -> Newsletter. However, there is no setting to disable newsletter success email. In this tutorial, we will tell you how to do that.
You can get the package of this module in the end of article.
Contents
- Where does Magento 2 send the subscription success email?
- 1. Create new file: /app/code/Metagento/DisableNewsletterSuccess/etc/di.xml
- 2. Create new file: /app/code/Metagento/DisableNewsletterSuccess/etc/adminhtml/system.xml
- 3. Create new file: /app/code/Metagento/DisableNewsletterSuccess/Plugin/Newsletter/Model/Subscriber.php
- 4. After creating all files, execute following commands to install the module.
- 5. Check again the configuration and enable the function
Where does Magento 2 send the subscription success email?
The sending email process can be found in file:
/vendor/magento/module-newsletter/Model/Subscriber.php method sendConfirmationSuccessEmail
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Newsletter\Model; class Subscriber extends \Magento\Framework\Model\AbstractModel { /** * Sends out confirmation success email * * @return $this */ public function sendConfirmationSuccessEmail() { if ($this->getImportMode()) { return $this; } /** * Check and Send email process */ return $this; } } |
Editing Magento core file is a bad option, your edited code will be removed when you upgrade Magento. So we will create a simple module to do that.
In this post, we just tell you how to disable subscription success email. You can read this post to know how to create an extension on Magento 2
Assuming that we already have an extension named “Metagento_DisableNewsletterSuccess“, let’s find out what to do next.
1. Create new file: /app/code/Metagento/DisableNewsletterSuccess/etc/di.xml
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Newsletter\Model\Subscriber"> <plugin name="disable_newsletter_success" type="Metagento\DisableNewsletterSuccess\Plugin\Newsletter\Model\Subscriber" /> </type> </config> |
2. Create new file: /app/code/Metagento/DisableNewsletterSuccess/etc/adminhtml/system.xml
This part will create a new setting, where you can enable/disable this function in the future.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Backend/etc/system_file.xsd"> <system> <section id="newsletter"> <group id="subscription"> <field id="disable_newsletter_success" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Disable Newsletter Confirmation Success Email</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> </group> </section> </system> </config> |
3. Create new file: /app/code/Metagento/DisableNewsletterSuccess/Plugin/Newsletter/Model/Subscriber.php
In this file, we will check the setting first. If the function is enabled, we set the import mode to stop sending emails.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php /** * Created by Metagento.com * Date: 11/1/2018 */ namespace Metagento\DisableNewsletterSuccess\Plugin\Newsletter\Model; class Subscriber { public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) { $this->scopeConfig = $scopeConfig; } public function beforeSendConfirmationSuccessEmail(\Magento\Newsletter\Model\Subscriber $subject) { if ($this->scopeConfig->getValue('newsletter/subscription/disable_newsletter_success', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) { $subject->setImportMode(true); } } } |
4. After creating all files, execute following commands to install the module.
1 2 3 |
php bin/magento setup:upgrade php bin/magento setup:static-content:deploy php bin/magento cache:flush |
5. Check again the configuration and enable the function
You can get the package of this module on github here: https://github.com/metagento/magento-2-disable-newsletter-success-email
We also provide a free extension which gives subscriber discount coupon codes.