1

I am trying to send dynamic value in my custom email template. I can send email with my custom template, but dynamic value not able to pass in that email. Here is my code for send email

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

        $retrievalcode = "somecustomcode";
        $parseDataVars->setData(array('recipient_name' => $retrievalcode));



        $this->temp_id = $this->getTemplateId(self::XML_PATH_EMAIL_TEMPLATE_FIELD);
        $this->inlineTranslation->suspend();    
        //$this->generateTemplate($emailTemplateVariables,$senderInfo,$receiverInfo);    

        $this->_transportBuilder->setTemplateIdentifier($this->temp_id)
            ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
         ->setTemplateVars(array('data' => $parseDataVars))        
        ->setFrom(
           $senderInfo
        )->addTo(
            $receiverInfo['email'],$receiverInfo['name']
        );

        $transport = $this->_transportBuilder->getTransport();
        $transport->sendMessage();        
        $this->inlineTranslation->resume();

My email template

<!--@subject Subject Of your email  @-->
<!--@vars
{"store url=\"\"":"Store Url",
"var recipient_name":"Recipient name",
"var sender_name":"Sender name",
"var sender_message":"Sender message",
"var initial_value":"Initial value",
"var currency_code":"Currency code"}
@-->

{{template config_path="design/email/header_template"}} <table cellspacing="0" cellpadding="0" class="message-container">
    <tr>
        <td>

            <br>
            You purchased a  {{var recipient_name}} at .<br>


            <br>
            Look forward to seeing you at our store.
        </td>
    </tr>
</table>

{{template config_path="design/email/footer_template"}} <!--footer of template-->

If anyone can know where i make mistake then please let me know why this code is not working.

Thank you

1 Answer 1

1

You need to pass values in templateVars like this

 $this->_transportBuilder->setTemplateIdentifier($this->temp_id)
        ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
     ->setTemplateVars(array(
      'data' => $parseDataVars,
      'recipient_name' => 'Name',
      'sender_name' => 'Sender Name',
      'currency_code' => 'currency_code',
      'initial_value' => 'initial_value',
      'sender_message' => 'Sender Message'
      ))        
    ->setFrom(
       $senderInfo
    )->addTo(
        $receiverInfo['email'],$receiverInfo['name']
    );

Let me know if you need further help.

5
  • Thanks of answer. But it is not working. Still i am not getting recipient_name in email template. As per you code, i got "Name" in my email. But only static text which is written in email template display in my email.
    – user55548
    Nov 6, 2018 at 5:55
  • I have use this code for sending email template webkul.com/blog/… What i found now I have put comment code of yourCustomMailSendMethod(). But still i got email. Do you know from where i get email. I think this is the reason.
    – user55548
    Nov 6, 2018 at 6:06
  • Do you need email in template or you talking about sender and recipient emails Nov 6, 2018 at 7:05
  • What i am trying is when customer subscribe to newsletter, i am sending new mail along newsletter email (which is default magento functionality). Newsletter mail working properly, but my new mail not able to sent.
    – user55548
    Nov 6, 2018 at 9:30
  • But your question is different @user55548. In question you said that you are not getting values in email template. Nov 7, 2018 at 10:34

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.