Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images Not Loading in Email from Rails App

Have some images that are stored in my rails assets/images folder. I need to display these images in an email sent from my rails app. I am able to send email but the images appear as broken links.

My development.rb file is shown below:

config.action_mailer.asset_host = 'http://localhost:3000'

config.action_controller.asset_host = 'http://localhost:3000'

My Mailer View is shown below:

<div class = "container">
  <%= image_tag(asset_path('com_name.jpg')) %>
</div>

<div class = "container">
    <%= image_tag(asset_path('banner.png')) %>
</div>

<h4>Hi! This mail is to inform you that your request has been successfully processed and the processed output has been attached along with your mail for your perusal</h4>

<h4>Looking forward to serve you once again.</h4>

<h5>Thanks,</h5>
<h4>Team Introhive</h4>

What am I doing wrong here? In the original email source i see the image tag referencing the correct fingerprinted files too but still it seems to be appearing broken.Any Help Please?

like image 458
Ahkshaey Ravi Avatar asked Oct 19 '16 08:10

Ahkshaey Ravi


1 Answers

Please try the following, In the mailer method,

For eg.

class NotifierMailer < ApplicationMailer
  def welcome(recipient)
    attachments.inline['photo.png'] = File.read('path/to/photo.png')
    mail(to: recipient, subject: "Here is what we look like")
  end
end

And in your views, use the following,

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

And if you want any options to be passed in you can use this,

<%= image_tag attachments['photo.png'].url, alt: 'Our Photo', class: 'photo' -%>

Please check the document http://api.rubyonrails.org/classes/ActionMailer/Base.html on inline images.

like image 117
Shabini Rajadas Avatar answered Oct 30 '22 23:10

Shabini Rajadas