How do I get full URL to an image in a Rails asynchronous mailer?

18,892

Solution 1

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.

Solution 2

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

Solution 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') %>" %>
Share:
18,892

Related videos on Youtube

Chris Aitchison
Author by

Chris Aitchison

I solve problems. Often with computers.

Updated on June 04, 2022

Comments

  • Chris Aitchison
    Chris Aitchison almost 2 years

    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
    Chris Aitchison about 11 years
    I've got those configured. Using Pow in dev, hence the .dev extension.
  • Austin Pray
    Austin Pray almost 10 years
    THANK YOU. This was the solution for getting rails 4.1.1 to display full image urls in HTML Emails.
  • Hertzel Guinness
    Hertzel Guinness about 9 years
    yet another underrated answers on SO. also, i think its better to use https in most cases which are not dev env.
  • lime
    lime almost 9 years
    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. :)
  • Chris Aitchison
    Chris Aitchison almost 9 years
    @lime what Rails versions are you working with? Glad to hear that the behaviour has changed :)
  • lime
    lime almost 9 years
    @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.
  • Adam Colvin
    Adam Colvin about 6 years
    Thank you for this answer. I'm using Rails 5.0.6 and still had this problem.