How To Get A List of Email Templates Programmatically In Magento 2
Recently, during a custom extension development, there was a requirement to send customers’ an Email regarding the price. And, for this, I had to load all the default Magento Email templates.
I used the below code to get a list of Email templates programmatically in Magento 2 and you can use it too, whenever you want to send an Email from the Magento 2 store.
Select the desired Email template from the list and hence the right Email will be sent accordingly to the customers.
Method to get a list of Email templates programmatically in Magento 2:
Create app/code/Vendor/Extension/Helper/Data.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace Vendor\Extension\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Email\Model\Template\Config; class Data extends AbstractHelper { private $emailTemplateConfig; public function __construct(Context $context, Config $emailTemplateConfig) { $this->emailTemplateConfig = $emailTemplateConfig; parent::__construct($context); } public function getEmailTemplateOptionArray() { return $this->emailTemplateConfig->getAvailableTemplates(); } } |
That’s it.
Feel free to mention your doubts about this method in the Comments section below. I’d be happy to help.
Do share the solution to fellow developers via social media.
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How To Get Related Products Collection In Magento 2
How To Add A Column To Existing Database Table In Magento 2
Next