Magento: Send Transactional Email

In Magento, Transactional Email Templates can be created from System -> Transactional Emails. This article shows
how we can send transactional emails programmatically.

There are few necessary things for any transactional email in Magento. They are:-

Template ID = ID of your transactional email template
Sender Name
Sender Email
Recepient Name
Recepient Email
Store ID = Current user’s store ID
Vars = Variables to use in transactional email template

Add a new template from System -> Transactional Emails and then you will see the ID of your template. That is your
Template ID to be used in code below.

You can write Sender Name, Sender Email, Recepient Name, and Recepient Email manually or you can the name and email from Store Email
Addresses (System -> Configuration -> GENERAL -> Store Email Addresses).

Store Email Addresses

In the code below, Sender Name and Email is taken from Store Config and Recepient Name and Email is written manually.

Custom variables for transactional email templates can be used with curly brackets like:-
{{var myVariableName}}

New email template

Here is the full code to send transactional email in Magento:-


public function sendTransactionalEmail()
{
	// Transactional Email Template's ID
	$templateId = 1;

	// 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);
	
	// Set recepient information
	$recepientEmail = 'john@example.com';
	$recepientName = 'John Doe';		
	
	// Get Store ID		
	$store = Mage::app()->getStore()->getId();

	// Set variables that can be used in email template
	$vars = array('customerName' => 'customer@example.com',
		      'customerEmail' => 'Mr. Nil Cust');
	        
	$translate  = Mage::getSingleton('core/translate');

	// Send Transactional Email
	Mage::getModel('core/email_template')
		->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);
	        
	$translate->setTranslateInline(true);	
}

Hope this helps. Thanks.