3

trying to send email programmaticaly with order items grid using {{layout handle="sales_email_order_items" order=$order}} but its not working. {{htmlescape var=$order.getCustomerName()}} show customer name properly.

what is missing?

following is code for email and 29 is id of template in which {{layout handle="sales_email_order_items" order=$order}} is mentioned.

Mage::app()->getLocale()->emulate($storeId);
$SalesOrder = Mage::getModel("sales/order")->load($requestParams['order_id']);
$storeEmail = Mage::getStoreConfig('trans_email/ident_custom1/email');
$storeName =  Mage::getStoreConfig('trans_email/ident_custom1/name');
$recvEmail = $CustomerName;
$recvName =  $email;
$templateId = 29;
//set data to use in array
$edata['comment'] = $ResultData->URL;
$edata['order'] = $SalesOrder;
//Core email sending code
$emailTemplate = Mage::getModel('core/email_template')->
                    addBcc('[email protected]')->
                    sendTransactional($templateId);
$emailTemplate->getProcessedTemplate($edata);
$emailTemplate->setSenderEmail($storeEmail);
$emailTemplate->setSenderName($storeName);
$emailTemplate->setType('html');
$emailTemplate->send($recvEmail,$recvName,$edata);

Thanks in advance.

Note: If you know any good tutorial on sending email programmatically with ordered item grid. please tell me.

2 Answers 2

6

I had the exact same problem, but was able to fix it by using store emulation.

It turned out the design package was incorrect. The email was being built using the adminhtml package instead of the frontend, even though I set "area=frontend" in the {{layout}}. That didn't help.

Before sending the email, you should add:

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

And after sending you should restore the original environment:

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

Be careful to reset the environment at the end, since the design model is a singleton, it may give unexpected results later on. You should put the email sending in try-catch, and restore afterwards. Don't let an exception prevent your from restoring the environment.

2

Try using this:

$emailTemplate = Mage::getModel('core/email_template')->loadDefault('yourtemplatenamehere');

$processedTemplate = $emailTemplate->getProcessedTemplate(array('order' => $order));
$processedSubject  = $emailTemplate->getProcessedTemplateSubject(array('order' => $order));

$mail = Mage::getModel('core/email')
           ->setToName('Someones name here')
           ->setToEmail($email)
           ->setBody($processedTemplate)
           ->setSubject($processedSubject)
           ->setFromEmail(Mage::getStoreConfig('trans_email/ident_sales/email'))
           ->setFromName(Mage::getStoreConfig('trans_email/ident_sales/name'))
           ->setType('html');

$mail->send();

From personal experience, this works and does what you expect from it.

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.