putyourlightson / craft-campaign

Send and manage email campaigns, contacts and mailing lists in Craft CMS.
https://putyourlightson.com/plugins/campaign
Other
63 stars 25 forks source link

Images in Sendouts Broken / Missing Hostname in URL #483

Closed adrianjean closed 4 months ago

adrianjean commented 4 months ago

Bug Report

Strange problem:

Code to display the image in my email template:

        {% set imageTransform = {
            mode: 'crop',
            width: 600,
            quality: 75            
        } %}

        {% do image.setTransform(imageTransform) %}
        {{ tag('img', {
            src: image.url,
            alt: image.alt
        }) }}

Any idea what's happening?

Plugin Version

3.4.0 pro

Craft CMS Version

5.2.5 pro

PHP Version

8.3.9

adrianjean commented 4 months ago

I simplified the image template code to this and still doesn't work:

{% set imageTransform = {
    mode: 'crop',
    width: 600,
    quality: 75            
} %}
{% do image.setTransform(imageTransform) %}
<img src="{{ image.getUrl() }}" width="{{image.width}}" style="width:{{image.width}}px;" />

Instead of getting: <img src="https://website.com/assets-media/images/_600xAUTO_crop_center-center_75_none/953009874.jpg" width="600" style="display: block; width: 600px;">

I get this: <img src="/assets-media/images/_600xAUTO_crop_center-center_75_none/953009874.jpg" width="600" style="display: block; width: 600px;">

Note the hostname / site URL is missing.

bencroker commented 4 months ago

It sounds like the base URL to the files in this filesystem that image assets use is a relative URL. Can you please ensure it is set to an absolute URL that contains the hostname?

adrianjean commented 4 months ago

oy... Yes, this pointed me in the right direction.

Because in my Settings -> Filesystems configuration for the filesystem I use @web in the Base URL field, I need to add in my /config/general.php a line to define the alias:

// Set aliases to use in the the CP
->aliases([
   '@web' => App::env('DEFAULT_SITE_URL'),
   '@webroot' => dirname(__DIR__) . '/web',
])

and then in my .env file define the variable:

DEFAULT_SITE_URL="https://website.com"

It's funny that when testing a sendout using the "Test" function with a test contact, it works without issue, but in the actual full live sending of the Sendout, the issue appears. Wonder why the functions are different.

bencroker commented 4 months ago

Probably because live sending happens via a queue job, which may be run from a console request, as opposed to a web request.