What is the right way to embed image into email using Rails?

后端 未结 6 1617
后悔当初
后悔当初 2020-12-02 16:23

What is the right way to embed an image into email using Rails?

6条回答
  •  时光说笑
    2020-12-02 17:23

    Copy pasted from here

    http://api.rubyonrails.org/classes/ActionMailer/Base.html#class-ActionMailer::Base-label-Inline+Attachments

    Inline Attachments

    You can also specify that a file should be displayed inline with other HTML. This is useful if you want to display a corporate logo or a photo.

        class Notifier < 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 then to reference the image in the view, you create a welcome.html.erb file and make a call to image_tag passing in the attachment you want to display and then call url on the attachment to get the relative content id path for the image source:

      

    Please Don't Cringe

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

    As we are using Action View's image_tag method, you can pass in any other options you want:

     

    Please Don't Cringe

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

提交回复
热议问题