Magento – How to set values in custom email templates

email-templatesmagento2

I am trying to customize new order email template for that I have done below code.

here is my email_templates.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="order_template" label="Template File" file="email_template.html" type="html" module="Pulsestorm_HelloWorldMVVM" area="frontend"/>
</config>

and here is my email_template.html

{{template config_path="design/email/header_template"}}
<table>
    <tr class="email-intro">
        <td>
            <p class="greeting">{{trans "%customer_name," customer_name=$myvar3}}</p>
            <p>
                {{trans "Your custom message."}}
                {{trans 'Your custom message <a href="%account_url">logging into your account</a>.' account_url=$this.getUrl($store,'admin',[_nosid:1]) |raw}}
            </p>
        </td>
    </tr>
    <tr class="email-summary">
        <td>
            <h1>{{trans 'Your Order <span class="no-link">#%increment_id</span>' increment_id=$myvar1 |raw}}</h1>
            <p>{{trans 'Placed on <span class="no-link">%created_at</span>' created_at=$myvar2 |raw}}</p>
        </td>
    </tr>
    <tr class="email-information">
        <td>
            <table class="order-details">
                <tr>
                    <td class="address-details">
                        <h3>{{trans "Billing Info"}}</h3>
                        <p>{{var myvar4|raw}}</p>
                    </td>
                </tr>
            </table>
            <table class="email-items">
                <thead>
                    <tr>
                        <th class="item-info">{{trans "Item"}}</th>
                        <th class="item-info">{{trans "Sku"}}</th>
                    </tr>
                </thead>
                <tbody>
                    {{var myvar8|raw}}
                </tbody>
            </table>
        </td>
    </tr>
</table>

{{template config_path="design/email/footer_template"}} 

here is my execute function (observer)

 public function execute(\Magento\Framework\Event\Observer $observer)
    {

    $orderIds = $observer->getEvent()->getOrderIds();
    $order = $this->_orderRepositoryInterface->get($orderIds[0]);
    $items =$order->getAllVisibleItems();
    $productIds = array();
    foreach($items as $item) {
        $productIds[]= $item->getProductId();
    }
   $customerEmail = $order->getCustomerEmail();
   $templateOptions = array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId());
   $templateVars = array(
            'store' => $this->storeManager->getStore(),
            'message'   => 'We processed your order ID We will contact you soon in mail for the acknowledgement if you not receive mail within 4 hours please get help from support@xxx.com'
        );
        $from = array('email' => "ramkishan.suthar@ranosys.com", 'name' => 'Ramkishan');
        $this->inlineTranslation->suspend();
        $to = array($customerEmail);
        $transport = $this->_transportBuilder->setTemplateIdentifier('order_template')
            ->setTemplateOptions($templateOptions)
            ->setTemplateVars($templateVars)
            ->setFrom($from)
            ->addTo($to)
            ->getTransport();
        $transport->sendMessage();
        $this->inlineTranslation->resume();

   }

now i want to ask that how can i set values in email_template.html

Best Answer

You can try as below:

$templateVars = array(
                'store' => $this->storeManager->getStore(),
                'message'   => 'We processed your order ID We will contact you soon in mail for the acknowledgement if you not receive mail within 4 hours please get help from support@xxx.com',
                'myvar2' => 'myvar2 value come here',
                'myvar3' => 'myvar3 value come here'
            );

In email template email_template.html, use like

{{var myvar2}}
{{var myvar3}}
{{trans "%customer_name" customer_name=$myvar3}}