Magento 1.9 Email – How to Create a New Email Template in Custom Module

controllersemailformsmagento-1.9

I created a new module to overwrite the AddressController.php controller.

Here I make some changes and i receive some data from the customer address form.
What I want to do now is to take all the data and send it via email.

My code for sending mail from controller looks like this:

$templateId = 17;
$emailTemplate = Mage::getModel('core/email_template')->load($templateId);
$mail = Mage::getModel('core/email');
$mail->setToName($customer->getId());

$email_template_variables = array(
   'first' => $first_name,
   'middlename' => $middle_name,
   'lastname' => $last_name,
   'company' => $company,
   'telephone' => $phone,
   'fax' => $fax,
   'street' => $street[],
   'city' => $city,
   'country_id' => $contry,
   'region' => $state
);
$mailSubject = 'Change billing address';
$from_email = "test@gmail.com";
$from_name = "testt";
$processedTemplate = $emailTemplate->getProcessedTemplate($email_template_variables);

$mail = Mage::getModel('core/email');
$mail->setToName('nume');
$mail->setTemplateParam($email_template_variables);
$mail->setToEmail('test@gmail.com');
$mail->setBody($processedTemplate);
$mail->setSubject($mailSubject);
$mail->setFromEmail($from_email);
$mail->setFromName($from_name);
$mail->setType('Html');

try {
    $mail->send();
    Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
} catch (Exception $ex) {
    Mage::getSingleton('core/session')->addError('Unable to send.');
}

Now my question is:

How can I create a new email template in my custom module and send those data?
Because if I try to create a new email template from System > Transactional Email. it asks me to load a existing template. But I don't want this. I want to create a new one in my theme.

Can anybody help me with a solution?

Best Answer

To define your own custom email template, you need to define it in app/code/<codepool>/<packagename>/<modulename>/etc/config.xml file.

Below is sample code:

<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>

After that you would need to create an HTML file under app/locale//template/email folder with name defined under tag as defined above.

Now you can use this file in your module.

There are many reference links to help you.

One is given below:

https://www.atwix.com/magento/email-sending-feature/

Note: Links and samples used in this answer are not for promoting any particular website. This is just for reference and to help faster.