anusha anusha - 7 months ago 20
Ruby Question

Images not loading in emails with CDN Hosts url rails

I recently moved all my app assets to CDN and everything is working fine. All images are loading in views. But my problem is my images are not loading in emails which I am sending from my application. I have specified

my CDN Path
in
production.rb
file also but still not working. When I inspect that image in email, I didn't even getting
src
for that image.

I am loading my images using
image_tag
.

Can anyone please help me on this:

Production.rb:

config.action_controller.asset_host = AssetsCdn.new
config.action_mailer.asset_host = AssetsCdn.new


config.action_mailer.default_url_options = { :host => 'localhost', :protocol => 'http' }
config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = false config.action_mailer.default :charset => "utf-8"
config.action_mailer.delivery_method = :smtp #:mailhopper


AssetsCdn Class:

require 'zlib'
class AssetsCdn
CSS_CDN = "cdn-css"
CSS_CDN_0 = "cdn-css0"
CSS_CDN_1 = "cdn-css1"
CSS_CDN_2 = "cdn-css2"

JS_CDN = "cdn-js"
JS_CDN_0 = "cdn-js0"
JS_CDN_1 = "cdn-js1"
JS_CDN_2 = "cdn-js2"

ASSETS_CDN = "cdn-assets"
ASSETS_CDN_0 = "cdn-assets0"
ASSETS_CDN_1 = "cdn-assets1"
ASSETS_CDN_2 = "cdn-assets2"
ALL = [CSS_CDN_0, CSS_CDN_1, CSS_CDN_2, JS_CDN_0, JS_CDN_1, JS_CDN_2, ASSETS_CDN_0, ASSETS_CDN_1, ASSETS_CDN_2]

def call(source, request = nil)
cdn_index = Zlib.crc32(source) % 3
if source.ends_with?('.css')
url("#{CSS_CDN}#{cdn_index}", request)
elsif source.ends_with?('.js')
url("#{JS_CDN}#{cdn_index}", request)
else
url("#{ASSETS_CDN}#{cdn_index}", request)
end
end

def url(cdn_subdomain, request = nil)
return "//#{cdn_subdomain}-#{localhost}:3000"
end
end


TIA.

Answer

action_mailer.assets_host doesn't accept protocol relative URLs. Instead URLs have to start with with a protocol like http:// or https://.

This makes kind of sense, because usually emails aren't opened in a browser and email clients are not able to inherit the protocol from a patent html document like browsers do.

Comments