Magento – Magento 1 – Newsletter subscription – send additional email

emailmagento-1.8newsletter

We have two newsletter tools in use (don't ask me why). The "old" one and the default one from Magento. For the old one to receive subscribers, we want to send an email with the address of the subscriber to a specified email address.

Hence, I need this:
When the user is subscribing and his data is added to Magento, it should als trigger an email to that specified email address (hardcoded, we will remove it later).

Where in the code can I achieve this, so that it covers all possible subscriptions (directly from page, when registering etc.)?

Best Answer

Currently Magento allows to send only one success email template by default. We can set this success email template via

System  >  Configuration  > Newsletter  >  Success Email Template

You can see that, it uses a dropdown section. This allows us to set only one email template for success mail. So you need to change this section to multiselct, so that you can select multiple success email templates. You again need to rewrite Mage_Newsletter's Subscriber model class in order to send multiple mails at a time.

So let us create a custom module, that do this for us. I love to call this module as Rkt_TwoNlSuccess.

Activation file of the module

File : app\etc\modules\Rkt_TwoNlSuccess.xml

<config>
    <modules>
        <Rkt_TwoNlSuccess>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Newsletter />
            </depends>
        </Rkt_TwoNlSuccess>
    </modules>
</config>

Note our module depends upon Mage_Newletter module. It is necessary.

System.xml file

This file defines system configuration definitions and alternation. We use this file to make success email template field multiselect instead of select.

File : app\code\local\Rkt\TwoNlSuccess\etc\system.xml

<config>
    <sections>
        <newsletter>
            <groups>
                <subscription>
                    <fields>
                        <success_email_template>
                            <label>Success Email Template</label>
                            <frontend_type>multiselect</frontend_type>
                            <source_model>adminhtml/system_config_source_email_template</source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </success_email_template>
                    </fields>
                </subscription>
            </groups>
        </newsletter>
    </sections>
</config>

Note we changed front type.

<frontend_type>multiselect</frontend_type>

So our first part is over. Now its time to do model rewrite

Rewrite Model file

We need to rewrite sendConfirmationSuccessEmail() method that is defined in the model class Mage_Newsletter_Model_Subscriber. This is important since the default code allows us to send only one mail. In order to do that you need to define two files.

File : app\code\local\Rkt\TwoNlSuccess\etc\config.xml

<config>
    <modules>
        <Rkt_Freepro>
            <version>1.0.0</version>
        </Rkt_Freepro>
    </modules>
    <global>
        <models>
            <newsletter>
                <rewrite>
                    <subscriber>Rkt_TwoNlSuccess_Model_Subscriber</subscriber>
                </rewrite>
            </newsletter>
        </models>
    </global>
</config>

This file just tells magento that, I need to rewrite the class Mage_Newsletter_Model_Subscriber with my custom class Rkt_TwoNlsuccess_Model_Subscriber. So magento will process this custom model class instead of default one. This is the great flexibility that Magento offers.

File : app\code\local\Rkt\TwoNlSuccess\Model\Subscriber.php

<?php
class Rkt_TwoNlSuccess_Model_Subscriber extends Mage_Newsletter_Model_Subscriber
{
    const XML_PATH_SUCCESS_EMAIL_TEMPLATE       = 'newsletter/subscription/success_email_template';
    const XML_PATH_SUCCESS_EMAIL_IDENTITY       = 'newsletter/subscription/success_email_identity';

    public function sendConfirmationSuccessEmail()
    {
        if ($this->getImportMode()) {
            return $this;
        }

        if(!Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE)
           || !Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY)
        ) {
            return $this;
        }

        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $translate->setTranslateInline(false);

        $email = Mage::getModel('core/email_template');

        $email_templates = explode(',', Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE));
        $identity = Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY);

        foreach ($email_templates as $template) {
            $email->sendTransactional(
                $template,
                $identity,
                $this->getEmail(),
                $this->getName(),
                array('subscriber'=>$this)
            );
        }


        $translate->setTranslateInline(true);

        return $this;
    }

}

Here we are rewriting the method sendConfirmationSuccessEmail(). The following code that does the trick here

        $email_templates = Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE);
        $identity = Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY);

        foreach ($email_templates as $template) {
            $email->sendTransactional(
                $template,
                $identity,
                $this->getEmail(),
                $this->getName(),
                array('subscriber'=>$this)
            );
        }

This code allows you to send any number of emails. So now you can send more than two success emails if you desire.

**side note :**Under System > Configuration > Newsletter > Allow guest Customers, If that values is set to Yes, then Magento will send success mails to only logged in customers who are trying to subscribe newsletter. For guest users, it will always send confirm mail.

In order to send success mail for all users, you need to set need confirm to NO.

Hope that will help

[This answer is a part of #mageStackDay. It is an event conducted by Magento Community members as part of increasing the question-answer ratio. For more information http://www.magestackday.com/]

Related Topic