Send a transactional email in Magento

Posted on December 22, 2012 by Nimrod Techn There have been 0 comments

Sometimes we would like to send a customized transactional email right after a transaction has been made.
How would we do it? Simple. Create an Observer and hook it up with Magento's checkout -success event (checkout_onepage_controller_success_action).

Let's create and extension dir (Transemail) with 2 files in it:

1. app/code/local/Engineering/Transemail/Model/Observer.php:
class Engineering_Transemail_Model_Observer extends Mage_Core_Model_Abstract {
    public function sendMail($observer)
		$event = $observer->getEvent();
		if(!$order = $event->getOrder())
			$order_Id = Mage::getSingleton('checkout/type_onepage')->getCheckout()
			$order = Mage::getModel('sales/order')->load($order_Id);
		$payment = $order->getPayment()->getMethod();

			$loginfos = false;	//shows informations on the progress data
			$addhistoryactive = false; //history and statuschange is saved in orders

			// Transactional Email Template's ID
			$templateId  = 1; //choise template

			// Set sender information
			$senderName = Mage::getStoreConfig('trans_email/ident_support/name');
			$senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');
			$sender = array('name' => $senderName,'email' => $senderEmail);

			// Get Store ID
			$storeid = Mage::app()->getStore()->getId();
			$billing_details = $order->getBillingAddress();
			$payment_data = $order->getData();
			$gender = $payment_data['customer_gender'];//$billing_details->getGender();
			$customer_name = $billing_details->getLastname();

				//add history comment
				$comment = 'blabla';

			{ //1-male, Magento standard
				$gender_html = "Mr. ".$customer_name;
			{ //0-female, Magento standard
				$gender_html = "Mrs. ".$customer_name;

			// Set recepient information
			$recepientEmail = $billing_details->getEmail();
			$recepientName = $billing_details->getFirstname()." "

			$payment = $order->getPayment();
			$method = $payment->getMethodInstance();
			$addinfo = $order->getPayment()->getAdditionalInformation();
			$grandtotal = $order->getGrandTotal();

			$paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
                        $paymentBlockHtml = $paymentBlock->toHtml();
			 // Set variables that can be used in email template
			$vars = array('customerName' => 'test',
					'customerEmail' => $recepientEmail,
					'gender_html' => $gender_html,
					'order' => $order,

			// Send Transactional Email
			$mailTemplate = Mage::getModel('core/email_template');
			$mailTemplate->setDesignConfig(array('area' => 'frontend'));
			$mailTemplate->sendTransactional($templateId, $sender, $recepientEmail,
                        $recepientName, $vars, $storeId);

        return true;
2. app/code/local/Engineering/Transemail/etc/config.xml:

<?xml version="1.0"?>

Now - Don't forget to create the app/etc/modules/Engineering_Transemail.xml file:
<?xml version="1.0"?>

This post was posted in Writing a Magento Extension

Magento is a well-engineered eCommerce platform designed to help engineers develop customized eCommerce online stores. Due to lack of proper coding documentation, was created with the sole purpose of instructing Magento developers to-be with the "how-to-do" know-how. In the event of unresolved issues, you are more than welcome to contact me for consultation. However, please do so only if you possess a Software Engineering background and you're able to specify your question.