Search code examples
phpemailmagentoconfirmation-email

Magento 1.8.1.0 Order Confirmation Emails - Send the email again to sales only


Our sales team didn't receive the order confirmation email for 5 different orders. The cause of this problem is still unknown but they are asking if we can send the order confirmation emails to them again, without sending it to the client.

The "Send email" button on the order detail page is correctly sending the mail but there is no way to prevent the client to receive it.

Is there some way we could send this email to the sales team only, whether it's via the back office or, if not possible, programmatically ?

Thank you for you answers


Solution

  • We finally decided to make a script to send the emails programatically. The script has been created in the following location:

    /shell/resendEmails.php
    

    You will only need to change the top-part parameters. The emails will be sent to this address instead of the customer's one. The Sales team will receive the copy has usual.

    The code itself is mostly a copy of the function Mage_Sales_Model_Order::sendNewOrderEmail() with a few modifications

    To execute the scrippt you need to access the page: http://YOUR-SITE-URL.com/shell/resendEmails.php

    (or whatever your script name is).

    <?php
            require '../app/Mage.php';
            Mage::app();
    
            /********************************
            *   Please modify the following parameters
            ********************************/
    
            //The orders for you wich you want to send again the confirmation email
            $order_numbers=array(
                'xxxxxxx',
                'yyyyyyy',
            );
    
            //Your email address (the email will be send to this address instead of to the customer's)
            $custom_email="[email protected]";
            $custom_name="YOUR NAME";
            /**********************************
            *   Please modify the above parameters
            **********************************/
    
    
            foreach ($order_numbers as $increment_id){
    
                $this_order = Mage::getModel('sales/order')->loadByIncrementId($increment_id);
    
                $storeId = $this_order->getStore()->getId();
    
    
                // Get the destination email addresses to send copies to
                $method = new ReflectionMethod(get_class($this_order), '_getEmails');
                $method->setAccessible(true);
                $send_to=$method->invoke($this_order,$this_order::XML_PATH_EMAIL_COPY_TO);
                $copyTo=$send_to;
    
                $copyMethod = Mage::getStoreConfig($this_order::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_order->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_order->getCustomerIsGuest()) {
                    $templateId = Mage::getStoreConfig($this_order::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
                    $customerName = $this_order->getBillingAddress()->getName();
                } else {
                    $templateId = Mage::getStoreConfig($this_order::XML_PATH_EMAIL_TEMPLATE, $storeId);
                    $customerName = $this_order->getCustomerName();
                }
    
                $mailer = Mage::getModel('core/email_template_mailer');
                $emailInfo = Mage::getModel('core/email_info');
                $emailInfo->addTo($custom_email, $custom_name);
                if ($copyTo && $copyMethod == 'bcc') {
                    // Add bcc to customer email
                    foreach ($copyTo as $email) {
                        $emailInfo->addBcc($email);
                    }
                }
                $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($this_order::XML_PATH_EMAIL_IDENTITY, $storeId));
                $mailer->setStoreId($storeId);
                $mailer->setTemplateId($templateId);
                $mailer->setTemplateParams(array(
                        'order'        => $this_order,
                        'billing'      => $this_order->getBillingAddress(),
                        'payment_html' => $paymentBlockHtml
                    )
                );
                $mailer->send();
            }?>