Today we are going to cover the way of setting up custom transactional email in Magento which you can customized using Admin Transactional email section in Magento.
Here is the step by step guide with bear minimal configuration -:
Step 1 – Create a new module under your local folder, in our case under /app/local/Scommerce/CustomEmail.
Step 2 – Register the module in etc/modules directory either in Scommerce_All.xml or Scommerce_CustomEmail.xml by using the following code -:
<br> <Scommerce_CustomEmail><br> <active>true</active><br> <codePool>local</codePool><br> </Scommerce_CustomEmail><br>
Step 3 – Create config.xml under etc directory of your module /app/local/Scommerce/CustomEmail/etc by using the following code
<br> <?xml version="1.0" encoding="UTF-8"?><br> <config><br> <modules><br> <Scommerce_CustomEmail><br> <version>0.0.1</version><br> </Scommerce_CustomEmail><br> </modules><br> <global><br> <template><br> <email><br> <scommerce_customemail_custom_email_template translate='label' module='customemail'><br> <label>Custom Email</label><br> <file>custom_email.html</file><br> <type>html</type><br> </scommerce_customemail_custom_email><br> </email><br> </template><br> </global><br> </config><br>
The above declaration will tell Magento that you have system configuration in the following xpath scommmerce_customemail -> custom ->email_template. You will see this in the next step in system.xml. Make sure it matches with system.xml otherwise it is not going to work. One of the important configuration to make this work.
Step 4 – Create system.xml under etc directory of your module /app/local/Scommerce/CustomEmail/etc by using the following code -:
<br> <?xml version="1.0"?><br> <config><br> <tabs><br> <scommerce translate="label" module="customemail"><br> <label>Scommerce Configuration</label><br> <sort_order>100</sort_order><br> </scommerce><br> </tabs><br> <sections><br> <scommerce_customemail translate="label"><br> <label>Custom Email</label><br> <tab>scommerce</tab><br> <frontend_type>text</frontend_type><br> <sort_order>100</sort_order><br> <show_in_default>1</show_in_default><br> <show_in_website>1</show_in_website><br> <show_in_store>1</show_in_store><br> <groups><br> <custom translate="label"><br> <label>Custom</label><br> <frontend_type>text</frontend_type><br> <sort_order>100</sort_order><br> <show_in_default>1</show_in_default><br> <show_in_website>1</show_in_website><br> <show_in_store>1</show_in_store><br> <fields><br> <sender_email_identity translate="label"><br> <label>Email Sender</label><br> <frontend_type>select</frontend_type><br> <source_model>adminhtml/system_config_source_email_identity</source_model><br> <sort_order>10</sort_order><br> <show_in_default>1</show_in_default><br> <show_in_website>1</show_in_website><br> <show_in_store>1</show_in_store><br> </sender_email_identity><br> <email_template translate="label"><br> <label>Email Template</label><br> <frontend_type>select</frontend_type><br> <source_model>adminhtml/system_config_source_email_template</source_model><br> <sort_order>20</sort_order><br> <show_in_default>1</show_in_default><br> <show_in_website>1</show_in_website><br> <show_in_store>1</show_in_store><br> </email_template><br> </fields><br> </custom><br> </groups><br> </scommerce_customemail><br> </sections><br> </config><br>
Step 5 – Sending transactional email either using your Model or Controller class
</p> <p> /**<br> * Sending email to customer<br> *<br> * @param type $name<br> * @param type $email<br> * @return boolean<br> */<br> private function sendEmail()<br> {<br> $storeId = Mage::app()->getStore()->getStoreId();<br> $emailId = $this->helper->getConfigData('sender_email_identity');<br> $emailFrom = $this->helper->getConfigData('email_template');<br> $email_address = 'toemailadddress@gmail.com'</p> <p> /* Sender Name */<br> $fromName =Mage::getStoreConfig('trans_email/ident_'.$emailFrom.'/name'); </p> <p> /* Sender Email */<br> $fromEmail = Mage::getStoreConfig('trans_email/ident_'.$emailFrom.'/email');</p> <p> try{<br> $translate = Mage::getSingleton('core/translate');</p> <p> /* @var $translate Mage_Core_Model_Translate */<br> $translate->setTranslateInline(false);</p> <p> $emailTemplate = Mage::getModel('core/email_template');</p> <p> /* @var $emailTemplate Mage_Core_Model_Email_Template */<br> $emailTemplate->setDesignConfig(array('area' => 'frontend', 'store' => $storeId));</p> <p> $emailTemplate->sendTransactional( $emailId,<br> array('email' => $fromEmail, 'name' => $fromName),<br> $email_address,<br> null,<br> array('custom_var1' => 'Custom Variable Value 1', 'custom_var2' => 'Custom Variable Value 2')<br> );</p> <p> $translate->setTranslateInline(true);<br> }<br> catch (Exception $e){<br> return false;<br> }<br> return true;<br> }</p> <p>
Step 6 – Create custom_email.html as declared in config.xml under app/locale/en_US/template/email
</p> <p><style type="text/css"><br> body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }<br> .orange { color:#DB7200; }<br> </style></p> <p><div style="font:11px/1.35em Verdana, Arial, Helvetica, sans-serif;"></p> <p> <table cellspacing="0" cellpadding="0" border="0" width="650"><br> <tr><br> <td valign="top"><br> <p><br> <strong>Hello <span class="orange">{{var custom_var1}}</span></strong>,<br/><br/><br> {{var custom_var2}}<br> </b><br> </p><br> <p><br> Thank you again for purchasing from My Website.<br> </p><br> Kind Regards</br></br></p> <p> </td><br> </tr><br> </table><br> </div><br>
That’s it, it is as simple as that. Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.