4

Can anyone help me how to include Pub/medial url in email templates in magento2.

Thanks

3 Answers 3

3

You should use below code:

<img src="{{media url=test/logo.svg}}" alt="" />
1
  • Is there any way where i can attach my order id in this like <a href="{{media url="'{{var order.increment_id}}'.pdf"}}" download>Please download PDF</a> but it didnt worked plz help me to get order id in media link ;) Mar 12, 2020 at 9:17
1

It should be the same as CMS block logic

So something like this

https://magento.stackexchange.com/a/94907/70343

But you can also add from extension. That might be a cleaner option? Something like this

Package/Modulename/view/frontend/web/images/customimg.png

<img src="{{view url='Package_Modulename/images/customimg.png'}}" height="30" width="30" alt="{{trans 'Custom image'}}" />

1

You can pass Url in setTemplateVars() like below,

public function __construct(
\Magento\Framework\Filesystem\DirectoryList $directory
){
    $this->directory = $directory;
}

You can get Media Url from below code,

$imageUrl  =  $this->directory->getPath('media');

Than after, you can pass in setTemplateVars() like shown below

$templateVars = array(
                        'imageUrl'   => $imageUrl
                    );

You can use in your template file like this

{{var imageUrl}}

Hope it helps.

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.