Reading list Switch to dark mode

    How to set dynamic email subject and content in Magento 2

    Updated 15 May 2023

    Sometimes we need to show customer name, order id, OTP etc in email subject or body.

    Here we will use the following Webkul Hello module to implement this blog.

    https://webkul.com/blog/create-hello-module-in-magento2/

    Step 1 -> Register template in email. Webkul/Hello/etc/email_templates.xml

    Start your headless eCommerce
    now.
    Find out More
    <?xml version="1.0"?>
    <!--
    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @package   Webkul_Marketplace
     * @author    Webkul
     * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    -->
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
        <template id="hello_template" label="Hello World" file="hello.html" type="html" module="Webkul_Hello" area="frontend"/>
    </config>

    Step 2 -> Load email content in Webkul/Hello/view/frontend/email/hello.html

    <!--
    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @package   Webkul_Marketplace
     * @author    Webkul
     * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    -->
    <!--@subject {{trans "Customer has uploaded attachment on %order_id" order_id=$orderId }} @-->
    <!--@styles
    body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
    @-->
    {{template config_path="design/email/header_template"}}
    <table cellspacing="0" cellpadding="0" border="0" width="100%">
        <tr>
            <td align="center" valign="top" style="padding:20px 0 20px 0">
                <table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                    <tr>
                        <td valign="top">
                            <h1 style="font-size:22px;font-weight:normal;line-height:22px;margin:0 0 11px 0;">{{trans "Hello"}},</h1>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <table cellspacing="0" cellpadding="0" border="0" width="650">
                                <tbody>
                                    <tr>
                                        <td colspan="2" valign="top" style="font-size:24px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                                <b>{{trans "Order Id: "}}{{var orderId}}</b>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                            {{trans "A Customer %customer_name has uploaded attachment in order %order_id" customer_name=$customer_name order_id=$orderId}}
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA;text-align:center;">
                            <center>
                                <p style="font-size:12px;margin:0;">
                                    <strong>{{trans "Thank you"}}</strong>
                                </p>
                            </center>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    {{template config_path="design/email/footer_template"}}

    Step 3 -> Load template with dynamic variables and send mail.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @package   Webkul_Marketplace
     * @author    Webkul
     * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    /** @var $storeManager \Magento\Store\Model\StoreManagerInterface $storeManager **/
    /** @var $transportBuilder \Magento\Framework\Mail\Template\TransportBuilder **/
    /** @var $inlineTranslation \Magento\Framework\Translate\Inline\StateInterface **/
    $templateOptions = [
        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
        'store' => $storeManager->getStore()->getId()
    ];
    $templateVars =  [
        'orderId' => "#00000009",
        'customer_name' => "Share Khan"
    ];
    $from = ['email' => "[email protected]", 'name' => 'Admin'];
    $inlineTranslation->suspend();
    $to = ['[email protected]'];
    $transport = $transportBuilder->setTemplateIdentifier('hello_template')
                    ->setTemplateOptions($templateOptions)
                    ->setTemplateVars($templateVars)
                    ->setFrom($from)
                    ->addTo($to)
                    ->getTransport();
    $transport->sendMessage();
    $inlineTranslation->resume();
    Screenshot-from-2023-05-12-15-39-46

    Thanks 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home