Question

Magento have functionality of adding emails to Bcc.But I want to add in cc.

    <?php

    class Mage_Core_Model_Email_Info extends Varien_Object
    {
        /**
         * Name list of "Bcc" recipients
         *
         * @var array
         */
        protected $_bccNames = array();

        /**
         * Email list of "Bcc" recipients
         *
         * @var array
         */
        protected $_bccEmails = array();

        /**
         * Name list of "To" recipients
         *
         * @var array
         */
        protected $_toNames = array();

        /**
         * Email list of "To" recipients
         *
         * @var array
         */
        protected $_toEmails = array();


        /**
         * Add new "Bcc" recipient to current email
         *
         * @param string $email
         * @param string|null $name
         * @return Mage_Core_Model_Email_Info
         */
        public function addBcc($email, $name = null)
        {
            array_push($this->_bccNames, $name);
            array_push($this->_bccEmails, $email);
            return $this;
        }

        /**
         * Add new "To" recipient to current email
         *
         * @param string $email
         * @param string|null $name
         * @return Mage_Core_Model_Email_Info
         */
        public function addTo($email, $name = null)
        {
            array_push($this->_toNames, $name);
            array_push($this->_toEmails, $email);
            return $this;
        }

        /**
         * Get the name list of "Bcc" recipients
         *
         * @return array
         */
        public function getBccNames()
        {
            return $this->_bccNames;
        }

        /**
         * Get the email list of "Bcc" recipients
         *
         * @return array
         */
        public function getBccEmails()
        {
            return $this->_bccEmails;
        }

        /**
         * Get the name list of "To" recipients
         *
         * @return array
         */
        public function getToNames()
        {
            return $this->_toNames;
        }

        /**
         * Get the email list of "To" recipients
         *
         * @return array
         */
        public function getToEmails()
        {
            return $this->_toEmails;
        }
    }

In Mage_Sales_Model_Order

public function sendNewOrderEmail() {
        $storeId = $this->getStore()->getId();

        if (!Mage::helper('sales')->canSendNewOrderEmail($storeId)) {
            return $this;
        }

        $emailSentAttributeValue = $this->hasEmailSent() ? $this->getEmailSent() : Mage::getModel('sales/order')->load($this->getId())->getData('email_sent');
        $this->setEmailSent((bool) $emailSentAttributeValue);
        if ($this->getEmailSent()) {
            return $this;
        }

        // Get the destination email addresses to send copies to
        $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
        $copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $storeId);

        // Start store emulation process
        $appEmulation = Mage::getSingleton('core/app_emulation');
        $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);

        try {
            // Retrieve specified view block from appropriate design package (depends on emulated store)
            $paymentBlock = Mage::helper('payment')->getInfoBlock($this->getPayment())
                    ->setIsSecureMode(true);
            $paymentBlock->getMethod()->setStore($storeId);
            $paymentBlockHtml = $paymentBlock->toHtml();
        } catch (Exception $exception) {
            // Stop store emulation process
            $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
            throw $exception;
        }

        // Stop store emulation process
        $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

        // Retrieve corresponding email template id and customer name
        if ($this->getCustomerIsGuest()) {
            $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
            $customerName = $this->getBillingAddress()->getName();
        } else {
            $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
            $customerName = $this->getCustomerName();
        }

        $mailer = Mage::getModel('core/email_template_mailer');
        $emailInfo = Mage::getModel('core/email_info');
        $emailInfo->addTo($this->getCustomerEmail(), $customerName);
        if ($copyTo && $copyMethod == 'bcc') {
            // Add bcc to customer email
            foreach ($copyTo as $email) {
                $emailInfo->addBcc($email);
            }
        }
        $customerId = $this->getCustomerId();
        $customerObject = Mage::getModel('customer/customer')->load($customerId);
        if($customerObject->getGroupId() == 2){

        $emailInfo->addCc($customerObject->getRepresentativeEmail());
        //$emailInfo->$mailer->getMail()->addCc('abc@gmail.com');
            $emailInfo->addBcc($customerObject->getRepresentativeEmail());
        }
        $mailer->addEmailInfo($emailInfo);

        // Email copies are sent as separated emails if their copy method is 'copy'
        if ($copyTo && $copyMethod == 'copy') {
            foreach ($copyTo as $email) {
                $emailInfo = Mage::getModel('core/email_info');
                $emailInfo->addTo($email);

                $mailer->addEmailInfo($emailInfo);
            }
        }

        // Set all required params and send emails
        $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
        $mailer->setStoreId($storeId);
        $mailer->setTemplateId($templateId);
        $mailer->setTemplateParams(array(
            'order' => $this,
            'billing' => $this->getBillingAddress(),
            'payment_html' => $paymentBlockHtml
                )
        );
        $mailer->send();

        $this->setEmailSent(true);
        $this->_getResource()->saveAttribute($this, 'email_sent');

        return $this;
    }

can you help anyone?

Was it helpful?

Solution

cc function existed in zend_mail class in lib folder.

public function addCc($email, $name='')
        {
            if (!is_array($email)) {
                $email = array($name => $email);
            }

            foreach ($email as $n => $recipient) {
                $this->_addRecipientAndHeader('Cc', $recipient, is_int($n) ? '' : $n);
            }

            return $this;
        }

For now i just edited core files.but we need to override to our local/community folder.I have created 2 arrays & 4 new functions for cc in info.php

<?php
class Mage_Core_Model_Email_Info extends Varien_Object
{
    /**
     * Name list of "Bcc" recipients
     *
     * @var array
     */
    protected $_bccNames = array();
    protected $_ccNames=array();

    /**
     * Email list of "Bcc" recipients
     *
     * @var array
     */
    protected $_bccEmails = array();
    protected $_ccEmails = array();
    /**
     * Name list of "To" recipients
     *
     * @var array
     */
    protected $_toNames = array();

    /**
     * Email list of "To" recipients
     *
     * @var array
     */
    protected $_toEmails = array();


    /**
     * Add new "Bcc" recipient to current email
     *
     * @param string $email
     * @param string|null $name
     * @return Mage_Core_Model_Email_Info
     */
    public function addBcc($email, $name = null)
    {
        array_push($this->_bccNames, $name);
        array_push($this->_bccEmails, $email);
        return $this;
    }
 public function addCc($email, $name = null)
    {
        array_push($this->_ccNames, $name);
        array_push($this->_ccEmails, $email);
        return $this;
    }
    /**
     * Add new "To" recipient to current email
     *
     * @param string $email
     * @param string|null $name
     * @return Mage_Core_Model_Email_Info
     */
    public function addTo($email, $name = null)
    {
        array_push($this->_toNames, $name);
        array_push($this->_toEmails, $email);
        return $this;
    }

    /**
     * Get the name list of "Bcc" recipients
     *
     * @return array
     */
    public function getBccNames()
    {
        return $this->_bccNames;
    }

    /**
     * Get the email list of "Bcc" recipients
     *
     * @return array
     */
    public function getBccEmails()
    {
        return $this->_bccEmails;
    }
/**
     * Get the name list of "cc" recipients
     *
     * @return array
     */
    public function getccNames()
    {
        return $this->_ccNames;
    }

    /**
     * Get the email list of "Bcc" recipients
     *
     * @return array
     */
    public function getccEmails()
    {
        return $this->_ccEmails;
    }
    /**
     * Get the name list of "To" recipients
     *
     * @return array
     */
    public function getToNames()
    {
        return $this->_toNames;
    }

    /**
     * Get the email list of "To" recipients
     *
     * @return array
     */
    public function getToEmails()
    {
        return $this->_toEmails;
    }
}

In Mage_Core_Model_Email_Template_Mailer

public function send()
    {
        $emailTemplate = Mage::getModel('core/email_template');
        // Send all emails from corresponding list
        while (!empty($this->_emailInfos)) {
            $emailInfo = array_pop($this->_emailInfos);
            // Handle "Bcc" recepients of the current email
            $emailTemplate->addBcc($emailInfo->getBccEmails());
            $emailTemplate->addCc($emailInfo->getccEmails());
            // Set required design parameters and delegate email sending to Mage_Core_Model_Email_Template
            $emailTemplate->setDesignConfig(array('area' => 'frontend', 'store' => $this->getStoreId()))
                ->sendTransactional(
                $this->getTemplateId(),
                $this->getSender(),
                $emailInfo->getToEmails(),
                $emailInfo->getToNames(),
                $this->getTemplateParams(),
                $this->getStoreId()
            );
        }
        return $this;
    }

add this function in Mage_Core_Model_Email_Template

public function addCc($cc)
    {
        if (is_array($cc)) {
            foreach ($cc as $email) {
                $this->getMail()->addCc($email);
            }
        }
        elseif ($cc) {
            $this->getMail()->addCc($cc);
        }
        return $this;
    }

OTHER TIPS

There is no cc equal in Magento as far as I know you instead can pass an array for the email addresses and an array for the names to the ->send() method:

$template = Mage::getModel('core/email_template')->loadDefault('customer_email');
$template->addSenderName("YourName");
$template->addSenderEMail("youremailinmagento@whatever.com");
$template->addBcc("some@email.com");
$template->send(array("ccmailaddress@some.com","another@email.com"),array("toname",$othername),$data);

No need to overwrite any function, try this

try{
      $mail = Mage::getModel('core/email_template');
      $mail->getMail()->addCc('abc@gmail.com');  

      $mail->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);
    }
    catch(Exception $e){
        print_r($e);
    } 

you have to use alternate way to send email as CC

because a class Mage_Core_Model_Email_Info seeems does not providing cc functionality so you can use it by

Unlike addBcc,addCc is not defined in class Mage_Core_Model_Email_Template. You can either extend Mage_Core_Model_Email_Template class to include addCc method in a similar fashion as addBcc, or modify your code like this:

// Send Transactional Email
try{
  $mail = Mage::getModel('core/email_template');
  $mail->getMail()->addCc('abc@gmail.com');  

  $mail->addBcc('abcd2@gmail.com')
  ->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);
}
catch(Exception $e){
    print_r($e);
}  

hope this will work for you

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top