CommercePeer Brand Logo


Programmatically: Magento 2 Product Entity

Programmatically: How to use Magento's custom email template system for custom email design

Step 1: Create a new email template

In your Magento theme, create a new folder email inside the app/design/frontend/your_theme/default directory. Then, create a new file custom_email.phtml inside the email folder.

Step 2: Define the email template

In custom_email.phtml, add the following code:


	echo '';
	echo '';
	echo '

Custom Email

'; echo ''; echo '';

Step 3: Create a custom email class

In your Magento module, create a new file etc/config.xml and add the following code:


	<?xml version="1.0"?>
	<config>
		<modules>
			<YourModule>
				<version>1.0.0</version>
			</YourModule>
		</modules>
		<global>
			<models>
				<email>
					<class>YOUR_MODULE_Model_Email</class>
				</email>
			</models>
		</global>
	</config>
	

Then, create a new file Model/Email.php in your Magento module and add the following code:


	class YOUR_MODULE_Model_Email extends Mage_Core_Model_Email_Template {
    public function sendEmail($templateName, array $vars) {
        // Set the email template
        $template = Mage::getModel('email/email_template')
            ->load('custom_email', 'name');

        // Set the template variables
        $template->setVars($vars);

        // Send the email
        $template->send();
    }
    }
	

Step 4: Use the custom email class

In your Magento module, create a new file etc/event/your_event.php and add the following code:


	class YOUR_MODULE_Model_Event_YourEvent {
    public function YourEvent() {
        // Get the email object
        $email = Mage::getModel('email/email');

        // Set the email template name
        $templateName = 'custom_email';

        // Set the template variables
        $vars = array(
            'name' => 'John Doe',
            'email' => 'johndoe@example.com'
        );

        // Send the email
        $email->sendEmail($templateName, $vars);
    }
    }
	

Step 5: Trigger the event

To trigger the event, you need to add a listener to your Magento module's etc/config.xml file:


	<?xml version="1.0"?>
	<config>
		<events>
			<your_event_name>
				<observers>
					<your_observer_name>
						<class>Your_Module_Model_Event_YourEvent</class>
						<method>YourEvent</method>
					</your_observer_name>
				</observers>
			</your_event_name>
		</events>
	</config>
	

LET’S WORK TOGETHER

We love working with Small Businesses, Retailers, Manufacturers. Our team is here to Help.

→ Schedule Free Consultant Now
Copyright © 2024 CommercePeer. All Rights Reserved.