Tuesday 17 June 2014

Magento add a custom email template

While creating a local module you may sometimes required to add the send mail functionality with custom email template.
Below is the code and description on how to create a send mail and loading it with a custom template with product data.

This tutorial for send email using custom template with product data like(Product name,description,image).

The following steps are for sending email with custom template feature for the module.

Step1:
Go to : app > code > community > Namespace > Modulename  > etc > system.xml
Add the following code:

<email_template translate="label">
    <label>Email Template for User</label>
    <frontend_type>select</frontend_type>
    <source_model>adminhtml/system_config_source_email_template</source_model>
    <sort_order>13</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
</email_template>


Step2:
Go to : app > code > community > Namespace > Modulename  > etc > config.xml
Add the following code:

<global>
    <template>
        <email>
            <modulename_general_email_template translate="label">
                <label>Pre-Order</label>
                <file>modulename_template.html</file>
                <type>html</type>
            </modulename_general_email_template>
        </email>
    </template>
</global>


Step3:
Go to : app > code > community > Namespace > Modulename > controllers > IndexController.php:
Add the following Email code in method:

<?php
    $storeId = Mage::app()->getStore()->getId();
    // get email template   
    $emailTemplate = Mage::getStoreConfig('modulename/general/email_template', $storeId);
   
    $translate = Mage::getSingleton('core/translate');
    $translate->setTranslateInline(false);   
    Mage::getModel('core/email_template')
    ->setDesignConfig(array('area'=>'frontend', 'store'=>$storeId))
    ->sendTransactional(
        $emailTemplate,
        'support',
        'test@test.com',
        '',
        array(
            'productcollection'     => "$productid",               
        ));
    $translate->setTranslateInline(true);
?>


Step4: Create custom template file
At location app > locale > en_US > template >  email > 
modulename_template.html
Add the following code:

<!--@subject custom Email for @-->
<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;">

<!--you can add template file like-->
 {{block type='core/template' area='frontend' template='modulename/email_tempate.phtml' productcollection=$productcollection }}

Thank you for sending email.
</div>
</body>


Step3: Create custom template file
At location: app > design > frontnend > default > default > template > modulename > email_tempate.phtml
Add the following code:

 <?php
$products = $this->getData('productcollection');
$obj = Mage::getModel('catalog/product');
$product = $obj->load($products);

if ($product): ?>
<p><?php echo $this->__('Message:') ?></p>
<table>
    <tr>
        <td><a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->escapeHtml($product->getName()) ?>"><img src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail')->resize(75, 75) ?>" border="0" align="left" height="75" width="75" alt="<?php echo $this->escapeHtml($product->getName()) ?>" /></a></td>
        <td>
            <p><a href="<?php echo $product->getProductUrl() ?>"><strong><?php echo $this->escapeHtml($product->getName()) ?></strong></a></p>
            <?php $shortDescription = $product->getShortDescription(); ?>
            <?php if ($shortDescription): ?>
                <p><small><?php echo $shortDescription ?></small></p>
            <?php endif; ?>
            <p><?php if ($product->getPrice() != $product->getFinalPrice()): ?>
                <?php echo $this->__('Regular Price:') ?> <strong style="text-decoration:line-through;"><?php echo Mage::helper('core')->currency($product->getPrice()) ?></strong><br />
                <strong><?php echo $this->__('Special price:') ?> <span style="color:#FF0000;"><?php echo Mage::helper('core')->currency($product->getFinalPrice()) ?></span></strong>
            <?php else: ?>
                <strong><?php echo $this->__('Price:') ?></strong> <?php echo Mage::helper('core')->currency($product->getPrice()) ?>
            <?php endif; ?></p>
        </td>
    </tr>
</table>
<?php endif; ?>


Hope this helps you.
Best of luck. 

No comments:

Post a Comment