Custom checkout cart – How to send email after successful checkout
10 Comments 18th FEB 2009 | Posted by Branko Ajzele in Magento

Recently I have been working on a custom checkout page for one of our clients in Sweden. I had some trouble figuring out how to send default Magento order email with all of the order info. After an hour or so of studying Magento core code, here is the solution on how to send email after successful order has been made.
< ?php
$order = new Mage_Sales_Model_Order();
$incrementId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order->loadByIncrementId($incrementId);
try
{
$order->sendNewOrderEmail();
} catch (Exception $ex) { }
?>
Not sure how useful this alone will be for you, so I’ll throw a little advice along the way. When trying to figure how to reuse Magento code, separate some time to study the Model classes with more detail. Then “tapping into” and reusing some of them should be far more easier.
Cheers…
To post code in comments, place your code inside [code] and [/code] tags.


















March 3rd, 2009 at 12:57
Thanks! You wouldn’t know how i can include transaction ID from DIBS which is stored in sales_order_entity_text – as far as i can see – into the email-template?
April 20th, 2009 at 4:09
Hi,
I am having problem using Buckaroo from opensphere.nl , I does not send new order email. After I create the invoice then it sends an email but not the first new order email. Pls help.
Here is the code:
/**
* Buckaroo Standard Checkout Controller
*
*/
class Mage_Buckaroo_StandardController extends Mage_Core_Controller_Front_Action
{
/**
* Order instance
*/
protected $_order;
//
// Flag only used for callback
// protected $_callbackAction = false;
protected function _expireAjax()
{
if (!Mage::getSingleton(‘checkout/session’)->getQuote()->hasItems()) {
$this->getResponse()->setHeader(‘HTTP/1.1′,’403 Session Expired’);
exit;
}
}
/**
* Get singleton with buckaroo strandard order transaction information
*
* @return Mage_Buckaroo_Model_Standard
*/
public function getStandard()
{
return Mage::getSingleton(‘buckaroo/standard’);
}
/**
* When a customer chooses Buckaroo on Checkout/Payment page
*
*/
public function redirectAction()
{
$session = Mage::getSingleton(‘checkout/session’);
$session->setBuckarooStandardQuoteId($session->getQuoteId());
$this->loadLayout();
$this->getLayout()->getBlock(‘content’)->append($this->getLayout()->createBlock(‘buckaroo/standard_redirect’));
$this->renderLayout();
}
/**
* When a customer cancel payment from buckaroo.
*/
public function cancelAction()
{
$session = Mage::getSingleton(‘checkout/session’);
$session->setQuoteId($session->getBuckarooStandardQuoteId(true));
$this->_redirect(‘checkout/cart’);
}
/**
* when buckaroo returns
* The order information at this point is in POST
* variables. However, you don’t want to “process” the order until you
* get validation from the IPN.
*/
public function successAction()
{
$session = Mage::getSingleton(‘checkout/session’);
$session->setQuoteId($session->getPaypalStandardQuoteId(true));
/**
* set the quote as inactive after back from paypal
*/
Mage::getSingleton(‘checkout/session’)->getQuote()->setIsActive(false)->save();
/**
* send confirmation email to customer
*/
$order = Mage::getModel(‘sales/order’);
$order->load(Mage::getSingleton(‘checkout/session’)->getLastOrderId());
if($order->getId()){
$order->sendNewOrderEmail();
}
//Mage::getSingleton(‘checkout/session’)->unsQuoteId();
$this->_redirect(‘checkout/onepage/success’, array(‘_secure’=>true));
}
//
// When callback is called from buckaroo
// just reflect to the success action
//
/* public function callbackAction()
{
$this->_callbackAction = true;
$this->successAction();
}
*/
///////////////////////////////////////////////////////////////////////
// functions to create the error page
///////////////////////////////////////////////////////////////////////
/**
* Get order
*
* @param none
* @return Mage_Sales_Model_Order
*/
public function getOrder()
{
if ($this->_order == null) {
$session = Mage::getSingleton(‘checkout/session’);
$this->_order = Mage::getModel(‘sales/order’);
$this->_order->loadByIncrementId($session->getLastRealOrderId());
}
return $this->_order;
}
public function errorAction()
{
$session = Mage::getSingleton(‘checkout/session’);
$errorMsg = Mage::helper(‘buckaroo’)->__(‘ An error occured during the paying process.’); //this is not dispalyed in the front end it all lies in the phtml file
$order = $this->getOrder();
if (!$order->getId()) {
$this->norouteAction();
return;
}
if ($order instanceof Mage_Sales_Model_Order && $order->getId()) {
$order->addStatusToHistory(
$order->getStatus(),
Mage::helper(‘buckaroo’)->__(‘Customer returned from Buckaroo.’) . $errorMsg
);
$order->save();
}
$this->loadLayout();
$this->renderLayout();
Mage::getSingleton(‘checkout/session’)->unsLastRealOrderId();
//’checkout/onepage/success’
//$this->_redirect(‘checkout/onepage/error’);
//return $errorMsg;
//echo ‘there was an error processing your order’;
}
}
November 3rd, 2009 at 16:44
Where can I edit this code in Magento, sorry totally newbie here!
November 26th, 2009 at 23:25
I’d really like to add this to my site but not sure where or what file It needs to be added to. Branko, could you give us an idea please?
November 27th, 2009 at 8:51
@Trev
This code is written for Magento 1.2. Recently I had an upgrade of the same site I implemented this code to latest Magento 1.3.2.4, and it seems to be working OK. To reply on the “what file It needs to be added to”… you can add it to any custom made view file and just call it like core/template type of block.
Cheers.
January 28th, 2010 at 18:06
Is it possible to send “thank you” e-mail after 2 weeks of purchase?
How could that be done?
March 14th, 2010 at 15:47
Thank you for that. I added it to the “checkout/onepage/success.phtml” at the top, and finally my TPV payment gateway sends an email to the client.
I still can’t get it to send me, the admin , an email, but this is great progress.
One question.. the word ‘try’ in the code, is that a PHP expression, or a comment?
Thanks again for saving my weekend.
March 15th, 2010 at 5:41
Another job well done by Branko…..
The ‘try’ and ‘catch’ blocks are a way in php to handle exceptions,to avoid unpredictable behavior in case of an error .Thus , these are not comments .Comments are always within // or /* */.
March 15th, 2010 at 8:53
Hi Mohit, thanks for explaining that… as you can tell I am only a hacker, not a programmer.
June 8th, 2010 at 10:23
Thanks for this work! Really helpful!
How could I adapt this code to send the invoice as attached file in the mail?