Jared Eitnier
Jared Eitnier

Reputation: 7152

Magento: email_template not loading in custom module

I've created my own custom contact form module. In postAction() function, in the IndexController.php I have

$mailTemplate = Mage::getModel('core/email_template');

Doing a console log shows that nothing gets stored in the variable. Why would this be? The default contacts module loads it fine, so why should anything be different here?

Also, when making the call to actually send the mail it always throws an Exception.

Upvotes: 0

Views: 920

Answers (1)

Vern Burton
Vern Burton

Reputation: 3210

const XML_PATH_EMAIL_RECIPIENT  = 'quotes/email/recipient_email';
const XML_PATH_EMAIL_SENDER     = 'quotes/email/sender_email_identity';
const XML_PATH_EMAIL_TEMPLATE   = 'quotes/email/email_template';

$mailTemplate = Mage::getModel('core/email_template');

/* @var $mailTemplate Mage_Core_Model_Email_Template */

                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->sendTransactional(
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                    null,
                    array('data' => $postObject)`
                );

This should allow you to send the template as you need it.

You will need this bit of XML in your config.xml

    <template>
        <email>
            <quotes_email_email_template translate="label" module="quotes">
                <label>Quote Form</label>
                <file>quote_form.html</file>
                <type>text</type>
            </quotes_email_email_template>
        </email>
    </template>


    <default>
        <quotes>
            <quotes>
                <enabled>1</enabled>
            </quotes>
            <email>
                <recipient_email><![CDATA[[email protected]]]></recipient_email>
                <sender_email_identity>custom2</sender_email_identity>
                <email_template>quotes_email_email_template</email_template>
            </email>
        </quotes>
    </default>

This is an example from Mage_Quotes module that allows a form to send a quote for a product.

Upvotes: 1

Related Questions