Rails 3.1: Trouble on displaying images in mailer view files
Solution 1
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"
Solution 2
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.
Solution 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.
Related videos on Youtube
user502052
Updated on June 04, 2022Comments
-
user502052 almost 2 years
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 over 12 yearsIt doesn't work for me. It generates the following HTML code:
<img src="http://assets/system/logo.png">
(without Fingerprinting and hostname). -
Mr_Nizzle over 12 yearsIf your
logo.png
is in the root of youimages
folder if it is into other folder inside images likesystem
you can add/assets/system/logo.png
-
Mr_Nizzle over 12 yearsor use the
asset_path
likeimage_tag(@root_url.to_s+asset_path('logo.png'))
-
Justin about 10 yearsDoes 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? -
Diego D over 8 yearsWhy 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 ;)