Email sending feature in the custom module

In our new article we would like to discuss how to make your own sending email feature for the module. Pretty often we need to have ability that allows us to notify customers or admins about some events, so it’s very important part of the module development. Moreover, as you may notice Magento has native email functionality which makes developer’s life simpler.
Let’s start. First of all, we should “explain” to our module that it has template, we can do it using config.xml, after that it is needed to add the following part to the config.xml file into the global scope:

<template>
    <email>
        <atwixmodule_email_template translate="label" module="atwixmodule">
            <label>Atwix Email</label>
            <file>atwix_email_template.html</file>
            <type>html</type>
        </atwixmodule_email_template>
    </email>
</template>

The following part translate=”label” module=”atwixmodule” means that Magento will translate <label> node using your module helper, and pay attention – don’t forget to declare it.
<file> is the name of future template and <type> is type, in our case it’s html, that shows that we can use HTML markup inside.
The next step is creating function that will send emails using our template, you can declare it everywhere you want, but follow good sense. Actually, it can be in model or helper, but in our case it is model, seeing native email functionality is build on using models. It is necessary to remind you about model declaration in config.xml as well.
So, there is model code:

class Atwix_Module_Model_Email extends Mage_Core_Model_Email_Template
{

    /**
     * Send email to recipient
     *
     * @param string $templateId template identifier (see config.xml to know it)
     * @param array $sender sender name with email ex. array('name' => 'John D.', 'email' => 'email@ex.com')
     * @param string $email recipient email address
     * @param string $name recipient name
     * @param string $subject email subject
     * @param array $params data array that will be passed into template
     */
    public function sendEmail($templateId, $sender, $email, $name, $subject, $params = array())
    {
        $this->setDesignConfig(array('area' => 'frontend', 'store' => $this->getDesignConfig()->getStore()))
            ->setTemplateSubject($subject)
            ->sendTransactional(
                $templateId,
                $sender,
                $email,
                $name,
                $params
        );
    }
}

Don’t miss one more important thing — creating email template. So it will be our next step, as template should be placed into app/locale/{locale_Code}/template/email/, where {locale_Code} is current/default locale of your store, and in this case template has name app/locale/en_US/template/email/atwix_email_template.html.
Notice for yourself, $params argument in the sendEmail function is located in the model. Using this variable we can pass data into template. How does it work? If you want to send some value into template, for example first name and last name, you should init $params variable see below:

$params = array(
    'firstname' => 'John',
    'lastname'  => 'D.'
);

and after this just call it in the template using specific way:

Hello {{var firstname}} {{var lastname}}

By the way you can pass object into templates, for example $_customer variable as instance of Mage_Customer_Model_Customer and use all methods available for this class in templates:

Hello {{var customer.getName()}}

And final, show you function calling that you can use everywhere:

Mage::getModel('atwixmodule/email')->sendEmail(
    'atwixmodule_email_template', 
    array('name' => 'John', 'email' => 'from@buddy.com'), 
    'to@buddy.com', 
    'Buddy', 
    'Atwix Test Email', 
    array('value' => 'Atwix')
);

That’s it, hope this article helps you to create perfect modules :)