magento2: Cron gets wrong base media URL

Preconditions

  1. Magento: 2.1.5
  2. PHP: 7

Steps to reproduce

  1. Set up Magento with pub folder as webroot
  2. Set email logo in Content > Design > Configuration > Edit > Transactional Emails > Logo Image
  3. Have a cron (custom) send an email

Expected result

  1. Email is received with all images showing.

Actual result

  1. No images show.
  2. Path to images contain: “/pub/”

The image above is set in config, but the issue will also happen for product image urls generated by:

return $this->_imageHelper->init($product, $type, $attributes)
            ->setImageFile($imageFile)->resize(208, 260)->save()->getUrl();

or images included with directives:

<img src="{{media url="wysiwyg/emails/mailer-footer.png"}}" alt="{{trans "Thank you, %store_name" store_name=$store.getFrontendName()}}!" />

You can force the media url in config: Stores > Configuration > Web But that shouldn’t be necessary.

Magento\Store\Model\Store->getBaseUrl(...) returns wrong value.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 15
  • Comments: 17 (10 by maintainers)

Most upvoted comments

We experienced the same problem, where the media paths in the generated sitemap.xml file were incorrect when the sitemap.xml was generated by the cronjob, if you generate it manually, the paths were correct.

Solution from @MrDaar fixed it for us, but it shouldn’t contain the forward slash in the middle, it should be:

  • {{unsecure_base_url}}media/
  • {{secure_base_url}}media/

Hi,

Thank you for your solutions, I’ve been debugging this problem for a moment now! The only thing is that I also had to change the web/(un)secure/base_static_url paths to {{(un)secure_base_url}}static/ to make it work.

WHAT IS THIS MAGIC?

Base URL for User Media Files {{unsecure_base_url}}media/ Secure Base URL for User Media Files {{secure_base_url}}media/

(edited)

Hi @nahuelsanchez, here is your Magento instance. Admin access: https://i-9111-2-3-develop.engcom.dev.magento.com/admin Login: admin Password: 123123q Instance will be terminated in up to 3 hours.

In have to add cron process in extension ,which can shown in a cron_schedule tabel. how can i add? in Magento1 Googleshopping Extension.

I can corroborate that setting the base_static_url path to {{(un)secure_base_url}}static/ solves the issue for me. The config that needs to be set probably depends if your images are in media or static folder.

I attempted that, but it didn’t seem to resolve the issue for me. Either I did something wrong, forgot to clear my cache or it’s another issue with single-store mode (maybe related to #8137).