Custom Email Templates in Magento

Hello,

I am explaining here the method for sending custom emails using the custom email templates with variables.

1) Create an email template from Admin-end > System > Transactional Emails, for example lets put this as Template Content

<!--@vars
{
"htmlescape var=$customer.name":"Customer Name",
"var customer.email":"Customer Email"
}
@-->
Dear {{htmlescape var=$customer.name}},<br/>        
Your E-mail</strong>: {{var customer.email}}

2) Suppose the id of the template created is 3

3) Below code you can write in any view file (.phtml) or in any model or controller (.php) file


<?php
				$customer = Mage::getModel('customer/customer')->load(2); //suppose the customer whose data to be fetched is 2
                if(!empty($customer)) 
               	{
               		$mailTemplate = Mage::getModel('core/email_template');
        			/* @var $mailTemplate Mage_Core_Model_Email_Template */
        	
        			$translate  = Mage::getSingleton('core/translate');
        			
               		$templateId = 3; //template for sending customer data
               		$template_collection =  $mailTemplate->load($templateId);     		            		
					$template_data = $template_collection->getData();
					if(!empty($template_data))
       				{
       					$templateId = $template_data['template_id'];
       					$mailSubject = $template_data['template_subject'];      					
       					
       					//fetch sender data from Adminend > System > Configuration > Store Email Addresses > General Contact
                 		$from_email = Mage::getStoreConfig('trans_email/ident_general/email'); //fetch sender email
						$from_name = Mage::getStoreConfig('trans_email/ident_general/name'); //fetch sender name
				
       					$sender = array('name'  => $from_name,
                  						'email' => $from_email);                  				
                  		
						$vars = array('customer'=>$customer); //for replacing the variables in email with data					
						/*This is optional*/
					  	$storeId = Mage::app()->getStore()->getId();
					  	$model = $mailTemplate->setReplyTo($sender['email'])->setTemplateSubject($mailSubject);
					  	$email = $customer->getEmail();
					  	$name = $customer->getName();				    				   		
       		   			$model->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId);				  		
	  	 				if (!$mailTemplate->getSentSuccess()) {
			                    throw new Exception();
	  	 				}
       					$translate->setTranslateInline(true);
       				}
               	}
?>

Hope this is helpful.

9 thoughts on “Custom Email Templates in Magento

  1. Working on the magento is not a big task and addition of emails templates increase the user interaction more.

  2. Hi, I need your help with this please: I edited my email templates on Magento but don’t remember deleting the existing ones. Can you please help me with pointers as to where I can find them please?

    Thank you.

  3. Hi

    I wonder if can help with a Magento question?

    Do you know how to add an image of the product to the order email template and a product link back to its product page on the Magento site? I can’t find any info on this, not sure if can be done, any help would be great.

    Thanks

    Dion

    1. @Dion – This cannot be done directly from order templates, you need to customize the item collection, kindly check this block – app/code/core/Mage/Sales/Block/Order/Email/Items

    2. @Dion — Hi!

      I also want to do the same. Have you found any solution?

      Please, reply!

      Regards!

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top