0

I have a helper class Email under vendor/module/model/helper/Email.php

I have to access a image in the module_path/view/frontend/web/images

I can't use $this->getViewFileUrl('vendor_module::images/image.jpg'); or $block->getViewFileUrl('vendor_module::images/image.jpg');

How can I call the image in my helper class?

Helper file

<?php

namespace Vendor\Module\Model\Helper;

use Magento\Framework\View\Asset\Repository;
use Magento\Framework\App\RequestInterface;

/**
 * Class Email
 * @package Vendor\Module\Model\Helper
 */
class Email
{
    protected $request;

    public function __construct(
        \Magento\Framework\App\Request\Http $request
    )
    {
        $this->request = $request;
    }
    /**
     * @param array $params
     */
    public function sendBasicmoduleFormMails(array $params)
    {
        $params = array('_secure' => $this->request->isSecure());
        $image = $this->assetRepo->getUrlWithParams('Vendor_Module::images/image.jpg', $params);
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= "From: <from_email>" . "\r\n" .

        $headers .= "X-Mailer: PHP/" . phpversion();
        $body = "Some email content with an {$image}";

        mail(some_email, 'a subject', $body, $headers);
    }
}
0

To get Image Path in your Helper or Controller, you need to use

use Magento\Framework\View\Asset\Repository;
use Magento\Framework\App\RequestInterface; // for $this->result

protected $request;

public function __construct(
      Repository $assetRepo,
      RequestInterface $request,
)
{
      $this->assetRepo = $assetRepo;
      $this->request = $request;
}

in your file. Once you add the repository and create object assetRepo & result, call image path with function,

$params = array('_secure' => $this->request->isSecure());
$this->assetRepo->getUrlWithParams('Vendor_Module::images/image.jpg', $params);

Refer to vendor\magento\module-payment\Model\CcConfig.php::getViewFileUrl($fileId, array $params = []) function

  • Thank you for your answer, i get the following error: Undefined property: Vendor\Module\Model\Helper\Email::$request – Frank Groot Oct 10 '17 at 14:39
  • Please check my updated answer – Abhishek Panchal Oct 10 '17 at 14:44
  • Thanks for your update, but i get the following error Uncaught TypeError: Argument 1 passed to Vendor\Module\Model\Helper\Email::__construct() must be an instance of Magento\Framework\App\Request\Http, none given Many thanks for your help – Frank Groot Oct 10 '17 at 14:46
  • can you please add your helper file? – Abhishek Panchal Oct 10 '17 at 14:50
  • Updated question – Frank Groot Oct 10 '17 at 14:57

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.