1

I'm trying to figure out how to send out emails from the order view when the automatic emails are disabled through the configuration. When pushing the send email buttons for the Invoice it confirms that an email is being sent, but nothing happens. Is there a possibility to send the mails manually when the automatic sales emails are disabled. When I enable them, all emails are sent, so there is no issue with mails in general.

Anyone an idea?

2 Answers 2

2

I don't believe there's a configuration setting for this, and it requires an extension to override default behavior. Are you familiar with writing extensions? I don't recommend directly modifying any core Magento files.

If you look at Mage_Sales_Model_Order::sendNewOrderEmail(), which is ultimately called when trying to send an order email from the admin, it looks like the admin's order email configuration setting is checked, as you suspected, in the Mage_Sales default helper. See below.

The above call refers back to Mage_Sales_Helper_Data::canSendNewOrderConfirmationEmail(), which you should extend and override. Place a condition in there to check to see if you're in the admin.

// This should work.
public function canSendNewOrderConfirmationEmail($store = null)
{
    $isInAdminArea = Mage::app()->getStore()->isAdmin();  // Points to Marius
    if ($isInAdminArea) {
        true;
    } else {
        return Mage::getStoreConfigFlag(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, $store);
    }
}
1
  • 1
    $isInAdminArea = Mage::app()->getStore()->isAdmin();
    – Marius
    Commented Oct 16, 2014 at 14:58
1

One way of accomplishing this would be to override Mage_Sales_Helper_Data::canSendNewOrderConfirmationEmail() but if you place any order from "Admin" then the sales email would still be automatically sent (which defeat your original reason of disabling it)

Another way to accomplish this would be to use observer and update your "Send Email" button title and link location

In your config.xml

    <admin>
    <routers>
        <adminhtml>
            <use>admin</use>
            <args>
                <modules>
                    <MagePal_Manualsendorderemail before="Mage_Adminhtml">MagePal_Manualsendorderemail_Adminhtml</MagePal_Manualsendorderemail>
                </modules>
            </args>
        </adminhtml>    
    </routers>
</admin>

<global> 
    <events>
        <adminhtml_block_html_before>
            <observers>
                <manualsendorderemail>
                    <class>manualsendorderemail/observer</class>
                    <method>updateButton</method>
                    <type>singleton</type>
                </manualsendorderemail>
            </observers>
        </adminhtml_block_html_before>
    </events>
</global>

In your observer

class MagePal_Manualsendorderemail_Model_Observer {

    public function orderPageButton(Varien_Event_Observer $observer) {
        $block = $observer->getEvent()->getData('block');

        if ($block->getId() == 'sales_order_view' && $block->getRequest()->getControllerName() == 'sales_order') {
            $_order = Mage::registry('sales_order');    
            $block->updateButton('send_notification', 'label', Mage::helper('catalog')->__('Manual Send Email'));
            $block->updateButton('send_notification', 'onclick', 'confirmSetLocation(\''
                 . Mage::helper('sales')->__('Are you sure you want to send Creditmemo email to customer?')
                 . '\', \'' . $this->getEmailUrl($_order) . '\')');

        }
    }

    public function getEmailUrl($order){

       // return custom admin controller url
    }

}

in your admin controller

include_once Mage::getModuleDir('controllers', 'Mage_Adminhtml') . DS . 'Sales' . DS . 'OrderController.php';

class ContempoSpace_AdminOrderConfirmation_Adminhtml_Sales_OrderController extends  Mage_Adminhtml_Sales_OrderController
{

    /**
     * Initialize order model instance
     *
     * @return Mage_Sales_Model_Order || false
     */
    protected function __initOrder()
    {
        $id = $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('manualsendorderemail/order')->load($id);

        if (!$order->getId()) {
            $this->_getSession()->addError($this->__('This order no longer exists.'));
            $this->_redirect('*/*/');
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);
            return false;
        }
        Mage::register('sales_order', $order);
        Mage::register('current_order', $order);
        return $order;
    }

    /**
     * Notify user
     */
    public function emailAction()
    {
        if ($order = $this->__initOrder()) {
            try {
                $order->sendNewOrderEmail();
                $historyItem = Mage::getResourceModel('sales/order_status_history_collection')
                    ->getUnnotifiedForInstance($order, Mage_Sales_Model_Order::HISTORY_ENTITY_NAME);
                if ($historyItem) {
                    $historyItem->setIsCustomerNotified(1);
                    $historyItem->save();
                }
                $this->_getSession()->addSuccess($this->__('The order email has been sent.'));
            } catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            } catch (Exception $e) {
                $this->_getSession()->addError($this->__('Failed to send the order email.'));
                Mage::logException($e);
            }
        }
        $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
    }

Extend order module

 class MagePal_Manualsendorderemail_Model_Order extends Mage_Sales_Model_Order{

  /**
     * Send email with order data
     *
     * @return Mage_Sales_Model_Order
     */
    public function sendNewOrderEmailManually()
    {
        $storeId = $this->getStore()->getId();

        // remove the check
     //   if (!Mage::helper('sales')->canSendNewOrderEmail($storeId)) {
     //       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);
            }
        }
        $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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.