17

I am using Ruby on Rails 3.1 and I would like to add my web site logo (that is, an image handled through the new Asset Pipeline) to an e-mail.

If in my mailer view file I state the following:

<% # Note: '@root_url' is my application hostname (eg: http://www.mysite.com) %>
<%= link_to image_tag( "#{@root_url.to_s}/images/logo.png"), @root_url.to_s %>

it doesn't work in production mode (that is, I cannot display the logo image) because I think the Asset Pipeline uses the Fingerprinting technique and in the received e-mail it doesn't. Inspecting the HTML logo element in the e-mail I get something like this:

<img src="http://www.mysitecom/images/logo.png"> # without Fingerprinting

How can I solve the problem?


In my production.rb file I have the following commented out code:

# Enable serving of images, stylesheets, and javascripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
user502052
  • 14,803
  • 30
  • 109
  • 188

10 Answers10

28

in config/environments/production.rb (and other enviroment files needed) add:

config.action_mailer.asset_host = 'http://mysite.com'

after that rails will automatically add hostname in front of paths generated by image_tag

# haml
= image_tag 'foo.jpg'

will become

#html
<img alt="" src="http://mysite.com/assets/foo.jpg" >

...same apply for image_path

#haml
%table#backgroundTable{background: image_path('email-background.jpg'), width: '100%', :border => "0", :cellpadding => "0", :cellspacing => "0"}

will become

<table background="http://mysite.com/assets/email-background.jpg" border="0" cellpadding="0" cellspacing="0" id="backgroundTable" width="100%">

watch out!!!

# this will make your emails display images
config.action_mailer.asset_host = 'http://mysite.com'

is different than

# this wont make your email images work
config.action_controller.asset_host = "http://mysite.com" 
equivalent8
  • 13,754
  • 8
  • 81
  • 109
  • Does anyone know if `config.action_mailer.asset_host = 'website'` is enabled by default in Rails 4? That is, do you only need to specify that option if you want the asset host to be something other than the root domain? – Justin Mar 12 '14 at 21:38
  • Why is not enabled by default? Or at least should be present this line, config.action_mailer.asset_host , just like config.action_controller.asset_host is! Anyway, thank you man! It works ;) – Diego D Dec 03 '15 at 15:57
9

All of these answers are assuming you're using the asset pipeline, but from your example, you're specifying an image in /public/images - this is not part of the asset pipeline, so all the asset_path based answers won't work, and further your initial fingerprinting supposition is incorrect.

If you put an image in /public/images, you want your image tag to have a src of http://yoursite.com/images/the-image.jpeg, no fingerprint, no asset path, nothing - just hard-code it into your view:

<img src="<%=@root_url%>/images/logo.png">

But, you have to actually have the file in that location! If you have your image in /app/assets/images, then you'll need to use image_tag and the asset pipeline as others have answered.

Irongaze.com
  • 1,639
  • 16
  • 22
3

An alternative is to include the image logo in the mail. The mail could also be viewed offline. You can add the logo in you Mailer class, with the following code..

attachments["your_logo.png"] = File.read("#{Rails.root}/assets/images/your_logo.png")

This code will include your image to the mail. I believe when you want to show your attachment in the mail you need to do the following:

Class YourMailer < ActionMailer::Base
def sendmail
.....
attachments.inline['your_logo.png'] = File.read("#{Rails.root}/assets/images/your_logo.png")
end

And in your sendmail.html.erb view you can use the image_tag method:

<%= image_tag attachments['your_logo.png'].url %>

note: if the mail does not get shown correctly you can alternatively try the solution at: Rails attachments inline are not shown correctly in gmail Your mail can then also be viewed offline correctly.

Community
  • 1
  • 1
Antek Drzewiecki
  • 544
  • 1
  • 7
  • 18
0

Have you tried adding something like this

config.action_mailer.default_url_options = { :host => 'www.example.com' }

to your config/enviroments/production.rb file

Max
  • 764
  • 7
  • 14
0

Try:

<%= link_to image_tag( "#{@root_url.to_s}/assets/logo.png"), @root_url.to_s %>
Mr_Nizzle
  • 6,644
  • 12
  • 55
  • 85
  • If your `logo.png` is in the root of you `images` folder if it is into other folder inside images like `system` you can add `/assets/system/logo.png` – Mr_Nizzle Dec 06 '11 at 17:03
  • or use the `asset_path` like `image_tag(@root_url.to_s+asset_path('logo.png'))` – Mr_Nizzle Dec 06 '11 at 17:06
0

You're giving image_tag an absolute url so it thinks it doesn't need to do any fingerprinting or anything else other than regurgitate the string you gave it. I would try

link_to( image_tag('logo.png'), @root_url)

You'll also need to set actioncontroller's asset host to get rails to generate a full url for the image rather than just a path

One caveat to note: if you change the image then the fingerprint will obviously change and so the inage URL in all of your previously sent emails will become invalid. You may wish to consider inline images, although obviously these increase the email size

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
0

Try out this one

<%= link_to image_tag( asset_path, 'logo.png'), @root_url.to_s %>
Michał Szajbe
  • 8,830
  • 3
  • 33
  • 39
Rameshwar Vyevhare
  • 2,699
  • 2
  • 28
  • 34
-1

Adding mode:'rb' worked for me:

attachments.inline['Logo.jpg'] = File.read(File.join(Rails.root,'app','assets','images','Logo.jpg'), mode: 'rb')
purplerice
  • 473
  • 1
  • 6
  • 22
-2

If you compile your assets:

RAILS_ENV=production bundle exec rake assets:precompile

and use asset_path in your view:

<%= link_to image_tag( asset_path('logo.png') ), @root_url.to_s %>

--it should work in both development and production. This is the way I do it my views, and .css.scss.erb stylesheets. I assume that it doesn't make a difference that it is a view for a mailer.

codesponge
  • 220
  • 2
  • 8
-2

make sure your html page have follwing header

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

and render image as:

<%= image_tag('http://'+@url+'/images/header.jpg')%>

or

if you want link to image then

<%= link_to image_tag('http://'+@url+'/images/header.jpg'),root_path %>

@url = 'your website address'

Harshal_m_joshi
  • 1,489
  • 1
  • 16
  • 27
  • @user502052 this might helpful to you http://api.rubyonrails.org/classes/ActionMailer/Base.html – Harshal_m_joshi Jan 04 '12 at 09:22
  • I'm not sure what you think the doctype declaration will do for a simple tag like this - images have been around for a while so its not an issue with the browser (or mail client) rendering the page differently – Matthew Savage Dec 03 '12 at 23:01
  • +@url+ is as non-ruby-way as it gets. Do not do that. It's not PHP. –  Jan 22 '16 at 10:41