27

I want to have emails I send out with ActionMailer contain images that link back to my app. In my development environment, for example:

<img src="http://myfullappurl.dev/assets/myimage.png">

I have this in my development.rb

config.action_controller.asset_host = 'myfullappurl.dev'
config.action_mailer.asset_host = config.action_controller.asset_host
config.action_mailer.default_url_options = { host: 'myfullappurl.dev', only_path: false }

But I can not get my mail templates to render a full URL in any of these way:

asset_path('myimage.png')
asset_path('myimage.png', only_path: false)
image_url('myimage.png')

A lot of similar questions on this topic are answered by doing something like this:

"#{request.protocol}#{request.host_with_port}#{asset_path('image.png')}"

But because my mails are sent asynchronously with Sidekiq, there is no request object like there would be if they were sent synchronously in a controller.

Is there an obvious thing I am missing, or do I have to fight against Rails to do this? I am using Rails 4, but I am sure that anything that works in 3.1/3.2 should do just fine.

Chris Aitchison
  • 4,656
  • 1
  • 27
  • 43

3 Answers3

46

Well this is odd behaviour, but I've resolved this issue. It turns out that unless the action_mailer.asset_host starts with http:// then it will be ignored. There is a Regex in actionpack/lib/action_view/helpers/asset_url_helper.rb that defines a valid ActionMailer URI:

URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}

However, if you put a http:// in front of the action_controller.asset_host then you will end up with links to http://http://myfullappurl.dev

So to resolve it I had to add the following to my development.rb

 config.action_controller.asset_host = 'myfullappurl.dev'
 config.action_mailer.asset_host = 'http://myfullappurl.dev'

Just to be clear, this is Rails 4.0.0beta1 with the emails being sent asynchronously with Sidekiq, I am not sure if this affects Rails 3.

Chris Aitchison
  • 4,656
  • 1
  • 27
  • 43
  • THANK YOU. This was the solution for getting rails 4.1.1 to display full image urls in HTML Emails. – Austin Pray Jun 23 '14 at 22:28
  • yet another underrated answers on SO. also, i think its better to use https in most cases which are not dev env. – Hertzel Guinness Apr 19 '15 at 11:58
  • I did not experience any issues when adding the protocol to `action_controller.asset_host`, not sure if that behaviour has changed. The fix itself works just like described. :) – lime Jun 30 '15 at 11:29
  • @lime what Rails versions are you working with? Glad to hear that the behaviour has changed :) – Chris Aitchison Jul 01 '15 at 03:36
  • @cmaitchison I'm on 4.2, though I didn't find any change in `asset_url_helper` that would explain the difference. Let's hope it doesn't reappear. – lime Jul 01 '15 at 09:18
  • Thank you for this answer. I'm using Rails 5.0.6 and still had this problem. – Adam Colvin Mar 11 '18 at 07:50
9

I believe you need to set these in your config

config.action_controller.asset_host = 'http://localhost:3000' #Or your domain
config.action_mailer.asset_host = config.action_controller.asset_host
Josh Wilson
  • 3,585
  • 7
  • 32
  • 53
3

in your environment file(i.e development.rb) :-

config.action_mailer.asset_host = 'http://localhost:3000' #Or your domain

in your mailer view file:-

<%= image_tag('image_name.jpg') %>

or

<img src="<%= image_url('image.jpg') %>" %>
sahilbathla
  • 519
  • 3
  • 10