Question

I'm working on some improvements inside PayPal extension for Magento and I have set to module send an email to customer after order status is changed. I have used the code below for each different status within PayPal order controller:

$order->sendOrderUpdateEmail();
$order->setEmailSent(true);

I would like to send custom email templates for each status, for example, when order is cancelled, the customer gets this message: "Hi dear customer, we are informing you that your order #123456 has been cancelled for some reason...". How can I use the functions above setting Magento to send an email calling the custom template I have created for that specific case? Is it possible?

Was it helpful?

Solution

Example Sending your Custom Email Template:

public function sendCustomMail()
{
    $emailTemplate  = Mage::getModel('core/email_template');
    $emailTemplate->loadDefault('custom_template_name');
    $emailTemplate->setTemplateSubject('my subject here');
    // Load from magento config..
    $email = Mage::getStoreConfig('trans_email/ident_general/email');
    $name = Mage::getStoreConfig('trans_email/ident_general/name');

    $emailTemplate->setSenderName($name );
    $emailTemplate->setSenderEmail($email);

    // Add some custom variables here to pass into the template.
    $emailTemplateVariables['username']  = ' something';
    $emailTemplateVariables['store_url'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
    $emailTemplate->send('customer@domain.com', 'name...', $emailTemplateVariables);
}

You will also need to add your custom template to your modules config

config.xml

<config>
    ...
    <global>
        ...
        <template>
            <email>
                <custom_template_name module="Namespace_Module">
                    <label>Custom Template</label>
                    <file>mycustomtemplate.html</file>
                    <type>html</type>
                </custom_template_name>
            </email>
        </template>
    </global>
</config>

You can then add your custom email template with the others

/app/locale/en_US/template/email/mycustomtemplate.html

<h1>Dear {{var username}}</h1>
<p>bla bla </p>
<div>{{var storename}} ({{var store_url}})</div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top