Getting a Transactional Email Template from Magento Adminhtml
Implementing transactional emails is a vital aspect of any e-commerce application like Magento. It allows you to send automated, personalized messages to customers based on specific triggers such as order placement, shipping confirmation, or password reset requests.
In this post, we’ll guide you through the process of configuring and sending a transactional email from your custom Magento module’s controller. Let’s dive into the details step-by-step.
Step 1: Configuring the Email Template in config.xml
Firstly, you need to define your email template configuration within the config.xml file of your module. The key areas to focus on are under <global><template><email></email></template></global>.
Here’s an example of how your configuration should look:
<global>
<template>
<email>
<customer_lowerprice_mail_toshop_template translate="label" module="telllowerpricelink">
<label>The mail from the TellLowerPriceForm to the shop</label>
<file>tellLowerPriceLink/toShopMail.html</file>
<type>html</type>
</customer_lowerprice_mail_toshop_template>
</email>
</template>
</global>
In this example, we’re defining a new email template named customer_lowerprice_mail_toshop_template. The <file> tag specifies the location of your HTML template file (in this case, tellLowerPriceLink/toShopMail.html), and the <type> tag indicates that it’s an HTML template.
Step 2: Configuring Email Template Selection in system.xml
Next, you’ll need to create a configuration field for selecting the email template in your module’s system.xml file. This will allow admins to choose which template should be used from the backend.
Here’s how to configure it:
<config>
<sections>
<customer translate="label" module="telllowerpricelink">
<groups>
<lowerprice_mail translate="label">
<fields>
<toshop_template translate="label">
<label>Template for the lower price mail which will be sent to the shop</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<!-- Other configuration fields -->
</toshop_template>
</fields>
</lowerprice_mail>
</groups>
</customer>
</sections>
</config>
In this example, we’re defining a toshop_template field under the lowerprice_mail group in the customer section. The <source_model> tag points to Magento’s built-in email template source model (adminhtml/system_config_source_email_template), which allows you to select from the available templates.
Step 3: Installing Configuration Values
During module installation, you must set the configured values using the install-0.1.0.php script. This ensures that your custom configuration is applied correctly when the module is installed.
Here’s an example of how to set the configuration data:
$configValuesMap = array(
'customer/lowerprice_mail/toshop_template' => 'customer_lowerprice_mail_toshop_template',
);
foreach ($configValuesMap as $configPath => $configValue) {
$installer->setConfigData($configPath, $configValue);
}
In this script, we’re mapping the configuration path (customer/lowerprice_mail/toshop_template) to its corresponding value (customer_lowerprice_mail_toshop_template). This ensures that the correct template is selected when admins configure your module.
Step 4: Sending Transactional Email from Controller
Finally, you can send a transactional email using the configured template in your controller. Here’s how to do it:
$this->toShopTemplateId = Mage::getStoreConfig('customer/lowerprice_mail/toshop_template');
var_dump($this->toShopTemplateId);
// Output: customer_lowerprice_mail_toshop_template
$this->toShopSubject = 'my test subject';
/* Will be set with the submitted mailaddress of the user */
$this->toShopSender = 'test@me.com';
$this->toShopRecipients = 'test2@me.com';
$toShopMailVars = Array();
/* instantiating the mailtemplatemodel and sending it */
$toShopMail = Mage::getModel('core/email_template')
->setTemplateSubject($this->toShopSubject)
->sendTransactional(
$this->toShopTemplateId,
$this->toShopSender,
$this->toShopRecipients,
null,
$toShopMailVars
);
var_dump($toShopMail->getId());
// Output: NULL (if the template ID is invalid or not found)
In this example, we’re retrieving the configured email template ID using Mage::getStoreConfig(). We then set up the subject, sender, and recipients for our transactional email. Finally, we use Mage::getModel('core/email_template')->sendTransactional() to send the email using the configured template.
Conclusion
Implementing transactional emails in Magento is a crucial aspect of providing a seamless customer experience. By following the steps outlined above â configuring your email templates in config.xml, creating configuration fields in system.xml, installing configuration values during module installation, and sending emails from your controller â you can effectively manage and send automated, personalized messages to your customers.
Remember that proper testing is essential before deploying transactional email functionality to a production environment. Always verify that the emails are being sent correctly with the expected content and formatting.
That’s it! With these guidelines, you should now be able to successfully configure and send transactional emails from your custom Magento module. If you found this post helpful, don’t forget to check out our other articles on Laravel Company for more insights into Magento development best practices.