Volkan
Volkan

Reputation: 546

Ruby on Rails 7 - Send inline image in email

I have a Ruby on Rails 7 app, running locally in development on my localhost:3000. I can send an email, but I would also like to include a logo image inside the email, inline, not downloadable attachment. Here is the code:

class XYZMailer < ApplicationMailer

  def create_xyz_email(first, second)
    @first = first
    @second= second
    attachments.inline['logo.png'] = File.read(Rails.root.join('app', 'assets', 'images', 'logo.png'))

    mail(to: @second.user.email, subject: "New XYZ #{@first.title} for you")

  end
end

I tried different options in html view, but none works:

<%= image_tag attachments['logo.png'].url, alt: "logo", style: "text-align: center; max-width: 96px;" %>
<img src="cid:logo.png" alt="logo" style="text-align: center; max-width: 96px;">
<%= image_tag attachments['logo.png'].url %>
<img src="cid:logo.png" alt="XYZ Logo" style="text-align: center; max-width: 96px;">
<img src="data:image/png;base64,YourBase64EncodedImageHere" alt="logo" style="text-align: center; max-width: 96px;">
<img src="data:image/png;base64,<%= Base64.encode64(File.read('app/assets/images/logo.png')).gsub("\n", '') %>" alt="XYZLogo">

Any idea on what I should do to make the image visible in email?

Upvotes: 0

Views: 83

Answers (0)

Related Questions