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



You should use below code:

<img src="{{media url=test/logo.svg}}" alt="" />
  • 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 '20 at 9:17

It should be the same as CMS block logic

So something like this


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


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


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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.