swapnesh
swapnesh

Reputation: 26732

How to send email to multiple users in magento

This is my code that is working fine when I'm sending it to one recipient, i.e if I'm using '[email protected]' (single email) it is working fine. However I want to include the second email id as well [email protected] and writing code like this: '[email protected],[email protected]' but this is not working.

Let me know how can I implement this functionality ?

$templateId = 1;
$sender = array(
    'name' => 'swapnesh',
    'email' => '[email protected]'
);
$store = Mage::app()->getStore();
$vars = array(
    'my_var' => 15,
    'another_var' => 12
);
$translate = Mage::getSingleton('core/translate');

// Send your email
Mage::getModel('core/email_template')->sendTransactional(
    $templateId,
    $sender,
    '[email protected],[email protected]',
    'Recipient Name',
    $vars,
    $store->getId()
);
$translate->setTranslateInline(true);

Upvotes: 4

Views: 8608

Answers (2)

Tim Bezhashvyly
Tim Bezhashvyly

Reputation: 9100

The answer is in Mage_Core_Model_Email_Template::send()

You can see that $email and $names arguments can be both arrays. So in your case if will be:

$recipients = [
    '[email protected]' => 'Recipient1 Name',
    '[email protected]' => 'Recipient2 Name'
];

Mage::getModel('core/email_template')->sendTransactional(
    $templateId,
    $sender,
    array_keys($recipients),
    array_values($recipients),
    $vars,
    $store->getId()
);

Upvotes: 8

iwane021
iwane021

Reputation: 15

For send Email to Multiple recipients in Contact Us Magento, you can tray this way.

Step One, you can Find this line on app/code/core/Mage/Contacts/Controllers/IndexController.php

$mailTemplate = Mage::getModel('core/email_template');
            /* @var $mailTemplate Mage_Core_Model_Email_Template */
            $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                ->setReplyTo($post['email'])
                ->sendTransactional(
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                    null,
                    array('data' => $postObject)
                );

and modified to bellow :

                $recipients = Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT);
                if ($recipients)
                {
                        $recipients = explode(";",$recipients);
                        if(count($recipients))
                        {
            foreach($recipients as $recipient) {

            $mailTemplate = Mage::getModel('core/email_template');
            /* @var $mailTemplate Mage_Core_Model_Email_Template */
            $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                ->setReplyTo($post['email'])
                ->sendTransactional(
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                    $recipient,
                    null,
                    array('data' => $postObject)
                );

                if (!$mailTemplate->getSentSuccess()) {
                throw new Exception();
                }
         }
     }
}

And don't forget to comment validation javascript on app/code/core/Mage/Contacts/etc/system.xml

Find this line :

<recipient_email translate="label">
    <label>Send Emails To</label>
    <frontend_type>text</frontend_type>
<!--<validate>validate-email</validate>-->
    <sort_order>10</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
</recipient_email>

Give comment to this line <validate>validate-email</validate>

this code works for me in magento 1.7.0.2 . I hope this help your problem.. :D

Don't forget to save and clear cache..

Upvotes: -2

Related Questions