Magento 1.9 – How to Get Custom Email Template by Name

emailemail-templatesmagento-1.9

I have following custom e-mail templates and want to send them to particular customer. enter image description here

My magento function as follows but fails to get relevant e-mail template.

$emailTemplate = Mage::getModel('core/email_template')->loadDefault('Newsletter subscription success new');


        $mail = Mage::getModel('core/email');
        $mail->setToName('John Customer');
        $mail->setToEmail('exa@gmail.com');
        $mail->setBody($emailTemplate);
        $mail->setSubject('The Subject');
        $mail->setFromEmail('my@my.com');
        $mail->setFromName("Your Name");
        $mail->setType('text');// You can use 'html' or 'text'

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

How I get the relevant e-mail template by name?

Best Answer

Here is the answer for your question.

$emailTemplate = Mage::getModel('core/email_template')->loadByCode('Newsletter subscription success new');

Try with following full code

        $emailTemplate = Mage::getModel('core/email_template')->loadByCode('Newsletter subscription success new');


        $emailTemplateVariables = array(
           'some_custom_variable' => 'Your Variable'
        );

        $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);

        $emailTemplate->setSenderName('Your Name');
        $emailTemplate->setSenderEmail('sender@somemail.com');
        $emailTemplate->setTemplateSubject("e-mail Subject");

        $emailTemplate->send('receiver@somemail.com', 'Your Name', $emailTemplateVariables);