Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5: image and loop of images are not showing in email

I have this in the mailer:

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

And in the email template I have this:

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

It works just fine and the logo does appear in the email.

Now this is the weird part, I have another image that is not in the asset pipeline but stored in the database and also a loop of other images stored in the database. And none of them appear in the email. The email goes through without any errors. Any idea what might be wrong and how I can debug this?

<%= image_tag @account.image.thumb.url %>
   <% @attachments.each do |attachment| %>
     <%= image_tag attachment.images.thumb.url %>
<% end %>

I'm using the gem carrierwave to attach image and I uploading this version:

version :thumb do
  process resize_to_fill: [1024, 768]
end
like image 581
Dev Avatar asked Oct 29 '19 09:10

Dev


Video Answer


1 Answers

There are two options here: either your images, stored in the databases, also have a public-facing url, and then you can use those. But most likely they do not, and then you have to add the images as attachments to your email as well, and then use the attachments when building the email.

So in your mailer write something like:

@attachments.each do |attachment|
  attachments.inline[attachment.image.original_filename] = attachment.image.thumb.read
end

and then in your mail view, you can just iterate over all @attachments again, and use the correct attachments.inline[...], something like

<% @attachments.each do |attachment| %>
  <%= image_tag attachments.inline[attachment.image.original_filename].url %>
<% end %>

Note: I am not entirely sure of the correct syntax here (you use images but I presume it should be singular? also you should check what is the best unique way for your image, maybe the original_filename is not ideal, you could also just use the id of the attachment.

like image 175
nathanvda Avatar answered Oct 16 '22 10:10

nathanvda