ruby-on-railsactionmailer

"Inline Attachments" images are not shown in mail with Action Mailer


Regards!

I'm trying to put Attachments Inline images into an Gmail email, but for some reason, images are not shown in the email's body.

I have searched a lot on the Internet and all found answers say same way to do it.

In my Mailer Class:

def send_mail
  attachments.inline['imagen.png'] = File.read(Rails.root.join("public", "borrar.png").to_path)
  mail(to: email, subject: 'test')
end

In my mailer view "send_mail.html.erb"

<%= image_tag attachments["imagen.png"].url %>

The email view

enter image description here

Ruby Version: ruby 3.2.2 Rails version: Rails 7.0.6 OS: Windows 11 Home.

I need your help please.

Thanks!


Solution

  • The problem was that "File.read" open the file in Text mode. I had to use File.open for opening the file in Binary mode.

    file = File.open(Rails.root.join("public", "borrar.png"), 'rb')
    attachments.inline['imagen.png'] = file.read