0

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

1

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);

        try{
            $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();

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

            $postObject = new \Magento\Framework\DataObject();
            $postObject->setData($requestData);

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

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

    }
}

Thanks

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

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.