How to Set Dynamic Email Subject in Magento 2
Magento store owners widely use Emails to communicate with users. Emails serve many purposes such as marketing, confirmation about store activities of customers, query solution. Also, customers can contact admin via Email for questions regarding products or services.
As we know that Emails have a noticeable impact in Magento stores, it’s template designing must be given attention. The subject of the Email is the first thing that recipient notices. It should be proper and relative to the email content.
For example, a customer has questions for a particular product and requests for a solution to the admin via the contact form on the product page. On click of the “submit” button, an Email gets sent to the admin with the query related to the product. In this scenario, If if the Email subject is set to the “Query For” + “name of the product”, admin finds it easy to approach the problem. Now, each product has the different name and to set such a dynamic subject, you need to custom code.
Hence a relative email subject is impactful. But it is not feasible to manually change the subject every time for each product or service. So here’s the solution to set dynamic email subject in Magento 2 via the template. We use the system variable to dynamically change the subject of the email and the below code shows its implementation.
Code to Set Dynamic Email Subject in Magento 2 via Template:
Vendor\Extensionname\view\frontend\email\youremailfile.html
1 |
@subject {{vardynamic_subject|raw }}@ |
in youremailfile html file.
Vendor\Extensionname\Controller\Index\Index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php public function execute() { try { $this->inlineTranslation->suspend(); $subject = "Dynamic Subject"; $templateOptions = ['area'=>\Magento\Framework\App\Area::AREA_FRONTEND,'store'=>\Magento\Store\Model\Store::DEFAULT_STORE_ID]; $templateVars = [dynamic_subject=>$subject]; $transport = $this->transportBuilder->setTemplateIdentifier('youremailfile')->setTemplateOptions($templateOptions)->setTemplateVars($templateVars)->setFrom($from)->addTo($to)->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); $this->messageManager->addSuccessMessage(__('Your Email Sent successfully')); $this->_redirect(' */*/'); }catch (\Exception $e) { $this->inlineTranslation->resume(); $this->messageManager->addErrorMessage(__('We can\'t process your request' . $e->getMessage())); $this->_redirect('*/*/'); } } |
Creating a dynamic subject line to help admin recognize the query or match the end users’ interests can boost Email open rates and interactions. Easily set the dynamic Email subjects accordingly with the above method!
You may mention your doubts in the comments section below. Rate the post with 5 stars if found useful ?
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.
4 Comments
Is it possible to send variable containing HTML in rendered form so that there’s no need to declare |raw in template file ?
Hello,
Yes, it’s possible to render HTML via variable.
However, you have to use |raw with that variable otherwise it will print the whole HTML as text like
.
Thank You.
hello sir,
i have try to add custom varible in order template but it’s not show in order confirm template
Hello,
Please refer https://meetanshi.com/blog/use-custom-variables-in-transactional-email-in-magento-2/ for the solution.
Thank you.