Sunday, July 12, 2015



Sending custom emails in Magento



Step 1: Create html file in app/locale/en_us/template/email/sales/sample_order.html

-> sample_order.html file in put your html (Mail sending) content.



Step 2: Adding your template to etc/config.xml to register the email template. 
-> app/code/[codePool]/[Namespace]/[Module]/etc/config.xml


   <config>
<global>
<template>
            <email>
                <sample_order translate="label" module="[Module Name]">
    <label>Sample Order</label>
    <file>sales/sample_order.html</file>
  <type>html</type>
                </sample_order>
            </email>
        </template>

</global>    
   </config>

Step 3: Sending the email  !

-> app/code/[codePool]/[Namespace]/[Module]/controllers/IndexController.php


//Send Custom Email Start
$emailTemplate = Mage::getModel('core/email_template')->loadDefault('sample_order');

$senderName = Mage::getStoreConfig('trans_email/ident_general/name');
$senderEmail = Mage::getStoreConfig('trans_email/ident_general/email');

$name = $data['name'];
$email = $data['email'];
$pro_name = $data['product_name'];
$pro_img  = $data['product_img'];
$pro_sku  = $data['product_sku'];
$pro_color  = $data['color'];  
//Variables for Confirmation Mail.
$emailTemplateVariables = array(
   'customer_name' => $name,
   'product_name'  => $pro_name,
   'product_img'   => $pro_img,
   'product_sku'   => $pro_sku,
   'product_color' => $pro_color
   
);

$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
  //Sending E-Mail to Customers.

$mail = Mage::getModel('core/email')
->setToName($name)
->setToEmail($email)
->setBody($processedTemplate)
->setSubject('Sample Order')
->setFromEmail($senderEmail)
->setFromName($senderName)
->setType('html');
try{
//Confimation E-Mail Send
$mail->send();
}
catch(Exception $error)
{
Mage::getSingleton('core/session')->addError($error->getMessage());
return false;
}

//Send Custom Email End


Setp 4: Your Mail Send Sucessfully.