In this article I am going to show you how to Create custom transactional email in magento and then after you can send your email email through this template with you custom magento module.I used to face to problem for sending email in magento .After getting proper solution then I thought I should have to post related to transactional email of magento. So that other can get benefit from it.So my topic for this article is Create custom transactional email in magento. But.But for custom transactional email you can create through magento backend side for this you should have email template in locale directory.But the magento admin side is for designing your existing email template to new great look or your desire email template.So lets see step by step how we can create custom transactional email in magento.
1)Declare email template in config.xml file
Navigate to dir : app/code/[CodePool]/[NameSpace]/[ModuleName]/etc/config.xml
In my case I have a module JRB_Customemail and code pool is local so dir will be: app/code/local/JRB/Customemail/etc/config.xml
config.xml:
<config> ................................ ................................ <gloabl> ................................. ................................. <template> <email> <!-- Unique template name that I can access to send email or to load email template--> <my_custom_email> <label>Custom email</label> <file>custome_email.html</file> <type>html</type> </my_custom_email> </email> </template> </gloabl> </config>
2)Create email template:
Navigate to dir : app/locale/[your_locale or en_US By deafult]/template/email/my_custom_email.html
In my case I have a module JRB_Customemail and code pool is local so dir will be: app/code/local/JRB/Customemail/etc/config.xml
<!--@subject My custom email template @--> <!--@vars {"store url=\"\"":"Store Url", "var logo_url":"Email Logo Image Url", "var logo_alt":"Email Logo Image Alt", "var data.custom_variable":"Custom variable"} @--> <!--@styles body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; } @--> <body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;"> <div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;"> <table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%"> <tr> <td align="center" valign="top" style="padding:20px 0 20px 0"> <!-- [ header starts here] --> <table bgcolor="FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;"> <!-- [ Header starts here] --> <tr> <td valign="top"> <a href="{{store url=""}}" style="color:#1E7EC8;"><img src="{{var logo_url}}" alt="{{var logo_alt}}" border="0"/></a> </td> </tr> <!-- [ Header ends here] --> <!-- [ middle starts here] --> <tr> <td valign="top"> <h1 style="font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;"">This is my custom email template</h1> <p style="font-size:12px; line-height:16px; margin:0 0 8px 0;"><strong>My custom variable is :</strong> {{var custom_variable}}</p> </td> </tr> <!-- [middle ends here]--> <!-- [Footer content starts here] --> <tr> <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA; text-align:center;"><center><p style="font-size:12px; margin:0;">Thank you again, <strong>{{var store.getFrontendName()}}</strong></p></center></td> </tr> <!-- [Footer content ends here] --> </table> </td> </tr> </table> </div> </body>
3) Sending email:
You can place this code anywhere wherever you want to trigger to send email.So from above we register our email template through config.xml and and save the content in our prefer locale folder having same name which I have declare in config.xml file.Now we can access or load the email template and send to prefer recipient through below code lets start.
< ?php // This is the template name from your etc/config.xml $template_id = 'my_custom_email'; // Who were sending to... $email_to = 'recipient@jyotiranjan.in'; $customer_name = 'Recipient'; // Load our template by template_id $email_template = Mage::getModel('core/email_template')->loadDefault($template_id); $custom_variable = 'my custom variable for my custom eamil template'; // Here is where we can define custom variables to go in our email template! $email_template_variables = array( 'custom_variable' => $custom_variable // Other variables for our email template. ); // In my case I am using the Store Name as sender name here. $sender_name = Mage::getStoreConfig(Mage_Core_Model_Store::XML_PATH_STORE_STORE_NAME); // In my case I am using the general store contact here as the sender email. $sender_email = Mage::getStoreConfig('trans_email/ident_general/email'); $email_template->setSenderName($sender_name); $email_template->setSenderEmail($sender_email); //Send the email! $email_template->send($email_to, $customer_name, $email_template_variables); ?>
So please don’t forget to share if have got any help 🙂