Magento 2 – Pass Variable and Output in Custom Email Template

emailemail-templatesmagento2

I am trying to pass data into my email template, something like this:
['catalogue_url' => 'http://url.html']

I am struggling to access and output it in the email, I have tried the following:

Test 1: {{var $catalogue_url|raw}}

Test 2: {{var $catalogue_url}}

Test 3: {{var $catalogue_url}}

Test 4: {{var catalogue_url}}

Test 5: {{catalogue_url}}

My email sending code:

    $transport = $this->_transportBuilder
        ->setTemplateIdentifier($code)
        ->setTemplateModel($model)
        ->setTemplateOptions([
            'area' =>  $area,
            'store' => $this->storeManager->getStore()->getId(),
        ])
        ->setTemplateVars(['data' => $data])
        ->setFrom($sender)
        ->addTo($to)
        ->getTransport();

    $transport->sendMessage();

I will try with $data.catalogue_url now.

What is wrong with this?

Best Answer

Try this:

$templateOptions = [
             'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
             'store' => $this->storeManager->getStore()->getId()
            ];
            $templateVars = [
                        'store' => $this->storeManager->getStore(),
                        'admin_name' => 'Admin',
                        'subject'    => 'subject',
                        'catalogue_url'    => 'pass url here'
                    ];
            $from = ['email' => 'from@email.com', 'name' => 'from name'];
            $to= "test@gmial.com"
            $this->inlineTranslation->suspend();
            $transport = $this->transportBuilder->setTemplateIdentifier('template name or id')
                    ->setTemplateOptions($templateOptions)
                    ->setTemplateVars($templateVars)
                    ->setFrom($from)
                    ->addTo($to)
                    ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();

Use in email template {{var catalogue_url}} variable