I have created custom module in my code with email_templates.xml its working fine but i need created template in admin then how to use that email template in code.

2 Answers 2


You can use like this.

Create your Class

namespace Vendor\Module\Controller\Index;

use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Controller\ResultFactory;

class Index extends \Magento\Framework\App\Action\Action

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        TransportBuilder $transportBuilder,
        array $data = [])
        $this->transportBuilder = $transportBuilder;
        return parent::__construct($context,$data);

    public function execute()
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

            $post = $this->getRequest()->getPostValue();
            $senderEmail = "[email protected]";
            $senderName = "Sender Name";
            $recipientEmail = "[email protected]";

            $identifier = 1;  // Enter your email template identifier here //get temptate id in your create in backend

            $requestData = array();

                $requestData['fname'] = $post['fname'];
                $requestData['address'] = $post['address'];
                $requestData['city'] = $post['city'];
                $requestData['state'] = $post['state'];
            // You can add more data as given above

            $postObject = new \Magento\Framework\DataObject();

            $transport = $this->transportBuilder
                ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID])
                ->setTemplateVars(['data' => $postObject])
                ->setFrom(['name' => $senderName,'email' => $senderEmail])

            $this->messageManager->addSuccess(__('Email has been sent successfully.'));
            return $resultRedirect;
        }catch(\Exception $e){
            $this->messageManager->addError(__('Something went wrong. Please try again later.'));
            return $resultRedirect;



  • Thanks @Jigar Patel, while using template id means we got solution Commented Oct 28, 2020 at 9:23

In admin, you have to load your custom email and then do your customization and then save it.

Then you need to create a store config like a new order email or invoice and then you have to select it over there.

If you are not interested to build the store config part using system.xml then just pass that email template id in setTemplateIdentifier()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.