Saturday, July 28, 2012

Magento send Custom Email

Magneto have default some of the already made Email Template .Now we also can make the Custom email Template used as per our need ,for standard of sending the Email.let's take an example of adding the Custom Email



First in our module create a system.xml file and write the following code
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <sections>
        <customer translate="label" module="mymodule">
            <groups>
                <custom_email translate="label">
                    <label>Custom Emails</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>5</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>0</show_in_website>
                    <show_in_store>0</show_in_store>
                    <fields>
                            <exist_user_template translate="label">
                                <label>Existing User Custom Email</label>
                                <frontend_type>select</frontend_type>
                                <source_model>adminhtml/system_config_source_email_template</source_model>
                                <sort_order>3</sort_order>
                                <show_in_default>1</show_in_default>
                                <show_in_website>1</show_in_website>
                                <show_in_store>1</show_in_store>
                            </exist_user_template>
                    </fields>
                </custom_email>
            </groups>
        </customer>
    </sections>
</config>

In this the only point to notice is this
<source_model>adminhtml/system_config_source_email_template</source_model>


which will store the data once saved in the core_config_data Table

This indicates our drop down will show a list of all our email templates.

Next we need to make add config.xml. Here will add our new template file. Inside our >global< tab in config.xml

<template>
            <email>
                <customer_custom_email_exist_template translate="label" module="mymodule">
                    <label>Existing User Custom Template</label>
                    <file>custom/mytemplate.html</file>
                    <type>html</type>
                </customer_custom_email_exist_template>
            </email>
        </template>

The above xml basically adds an entry into Magento Transactional Email.

This >customer_custom_email_exist_user_template< should be same as the entire path written is system.xml. New we need to add a file at folder app/locale/en_US/template/email/custom/mytemplate.html and put your default email content there if any. This file is mentioned in the >file< above.

After doing the above steps go to Admin -> System -> Transactional Email and you should be able to see your email template created in the Load Template drop down .

You can put in the content and subject of the email in admin. Also, when you go to Admin -> System -> Configuration -> Customer -> Quote Email, the drop down we just created, you will see our new email already selected there. Just save the configuration again at this point.
Next, to send the email from your module this the function to use

const XML_PATH_EMAIL_ADMIN_QUOTE_NOTIFICATION = 'customer/custom_email/exist_template';

public function _sendNotificationEmail($to, $templateConfigPath = self::XML_PATH_EMAIL_ADMIN_CUSTOM_NOTIFICATION)
    {
        if (! $to) return;
        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $translate->setTranslateInline(false);
        $mailTemplate = Mage::getModel('core/email_template');
        /* @var $mailTemplate Mage_Core_Model_Email_Template */
        $template = Mage::getStoreConfig($templateConfigPath, Mage::app()->getStore()->getId());
        $sendTo = array();
        foreach ($to as $recipient)
        {
            if (is_array($recipient))
            {
                $sendTo[] = $recipient;
            }
            else
            {
                $sendTo[] = array(
                    'email' => $recipient,
                    'name' => null,
                );
            }
        }
        foreach ($sendTo as $recipient) {
            $mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>Mage::app()->getStore()->getId()))
            ->sendTransactional(
            $template,
            Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_IDENTITY,Mage::app()->getStore()->getId()),
            $recipient['email'],
            $recipient['name'],
            array(
                    'customer'  => $customer,
                     'quote' => $quote
            )
            );
        }
        $translate->setTranslateInline(true);
        return $this;
    }

You need to call this function and call the correct parameters. $to is an array of recipient name and email id, $templateConfigPath is the path of our system.xml drop down.
Call this function from your model or controller, and this would send the email. The mail function used here is sendTransactional()
sendTransactional(
            $template,
            Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_IDENTITY,Mage::app()->getStore()->getId()),
            $recipient['email'],
            $recipient['name'],
            array(

                    'customer'  => $customer,
                    'quote' => $quote
            )
            )

The definition of this function is
public function sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)

Passing Parameters to Email Template
As you can see above we have passed an array with $customer and $quote variable in the sendTransactional() function. These parameters passed can access in our email template. Like if we want to show the show the customer name and email in our template we need to put this in our template

{{htmlescape var=$customer.name}}

So, this is how we can pass variables to our email templates and access them.

2 comments:

  1. Great post, you can wrap it up in tiny magento module....
    Magento Modules

    ReplyDelete
  2. Superb info.

    Please contact us
     Renderindia Infotech Bangalore
    magento e commerce development company Bangalore

    ReplyDelete

Thankyou for your Comments

LinkWithin

Blog has moved, searching new blog...