Magento: Magento Send Custom Email
In this article I’ll explain how to send custom email in magento with custom template.If you are a magento developer you are already aware that magento sends email on certain event.
Sometimes you may find yourself having to send an email after a particular action or event. While creating a local module you may sometimes required to add the send mail functionality on success of your task.
You can manage all email template using “System->Transaction Email” section well.
First of all question is why you need to create custom email ? It has tow reason. First your webmaster/client can modify content or design of your email. Other you can add some variable in custom email which your webmaster /client can use in any place in the template, so it will make your webmaster/client easy to play with email.
Template creation involve 2 work. First create .html format of your email and second is add your email information in your module config.xml.
Follow following steps:-
Step 1) Adding your template to etc/config.xml to register the email template.
First of all, assuming you already have a basic module setup, add this into app/code/[local]/[Namespace]/[Module]/etc/config.xml
<config> ... <global> ... <template> <email> <!-- Give the template a uniqiue name, you'll need to refer to this later when sending the email--> <[email_template_name]> <label>[Email Template Label]</label> <file>[email_template_filename].html</file> <type>html</type> </[email_template_name]> </email> </template> </global> </config>
Step 2) Creating your email template
We now have the email template setup, next up we’ll need to create the email template itself. This file needs to be saved in app/locale/[your_locale_or_en_US]/template/email/[email_template_filename].html. The locale code is important, this allows you to create the email in multiple languages, the default locale in Magento is en_US, so saving there is a good place if you’re not sure.
<!--@subject This is the subject line of the email! @--> <div style="font:11px/1.35em Verdana, Arial, Helvetica, sans-serif;"> <table cellspacing="0" cellpadding="0" border="0" width="98%" style="margin-top:10px; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; margin-bottom:10px;"> <tr> <td align="center" valign="top"> <!-- [ header starts here] --> <table cellspacing="0" cellpadding="0" border="0" width="650"> <tr> <td valign="top"> <p> <a href="{{store url=""}}" style="color:#1E7EC8;"><img src="{{skin url="images/logo_email.gif" _area='frontend'}}" alt="Magento" border="0"/></a> </p> </td> </tr> </table> <!-- [ middle starts here] --> <table cellspacing="0" cellpadding="0" border="0" width="650"> <tr> <td valign="top"> <p> <strong>Dear {{var customer_name}}</strong>,<br/> This is the content of your email! </p> </td> </tr> </table> </td> </tr> </table> </div>
Since these templates don’t support PHP directly, we can use this form accessing variables and objects.
Get the current stores URL
{{store url=””}}
Get the skin url, accepts a path, in this case it’s the logo image.
{{skin url=”images/logo_email.gif”}}
Custom variables, as defined by us. We’ll cover this shortly!
{{var customer_name}}
Step 3) Sending the email
We have our email template configured, and recognised by Magento. Now it’s time to put it all together and send the email!
<?php // This is the template name from your etc/config.xml $template_id = '[email_template_name]'; // Who were sending to... $email_to = 'info@prashantblog.com'; $customer_name = 'prashant Kumar'; // Load our template by template_id $email_template = Mage::getModel('core/email_template')->loadDefault($template_id); // Here is where we can define custom variables to go in our email template! $email_template_variables = array( 'customer_name' => $customerName // Other variables for our email template. ); // I'm using the Store Name as sender name here. $sender_name = Mage::getStoreConfig(Mage_Core_Model_Store::XML_PATH_STORE_STORE_NAME); // I'm 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); ?>
And that’s it, that is how you send a transactional email in Magento.
Enjoy. If you have any query regarding this please feel free to contact me.