What is the correct way to embed images in email using Rails?

What is the correct way to embed an image in email using Rails?

+45
ruby-on-rails actionmailer
Feb 07 2018-11-11T00:
source share
6 answers

I combined the answer from Oksana with a custom helper approach and got the following to work pretty well.

app/helpers/email_helper.rb

 module EmailHelper def email_image_tag(image, **options) attachments[image] = File.read(Rails.root.join("app/assets/images/#{image}")) image_tag attachments[image].url, **options end end 

app/mailers/base_mailer.rb

 class BaseMailer < ActionMailer::Base add_template_helper(EmailHelper) end 

app/mailers/my_mailer.rb

 class MyMailer < BaseMailer def send_my_mail(email) mail to: email, subject: "My Subject" end end 

Then, for example, if I want to add a company logo to my email layout file, I would use

app/views/layouts/email.html.erb

<%= email_image_tag("company_logo.png") %>




Please note that ** options make the tag more extensible, but it only works in ruby> = 2. To make this work in ruby ​​<2 you will have to use the older method of processing keywords.

+50
Sep 12 '14 at 2:03
source share

Adding answers to Oksana and tdubs

The tdubs module writes works on the desktop, but for a mobile gmail client, images are displayed as attachments. To fix it, do it for

Application / helpers / email_helper.rb

 module EmailHelper def email_image_tag(image, **options) attachments[image] = { :data => File.read(Rails.root.join("app/assets/images/emails/#{image}")), :mime_type => "image/png", :encoding => "base64" } image_tag attachments[image].url, **options end end 

For the rest, follow the answers to tdubs.

+19
May 13 '15 at 2:33
source share

RAILS 5

In your mail method, add a built-in attachment pointing to your image:

 class ConfirmationMailer < ActionMailer::Base def confirmation_email attachments.inline["logo.png"] = File.read("#{Rails.root}/app/assets/images/logo.png") mail(to: email, subject: 'test subject') end end 

Then in your html mail, view the image_tag with the application url:

 <%= image_tag(attachments['logo.png'].url) %> 
+15
Sep 26 '16 at 19:04
source share

After much research, I found a very clean way to embed images in email. Just add the following line to production.rb and development.rb

 config.action_mailer.asset_host = 'YOUR HOST URL' 

In your opinion, insert the image using the following code.

 <%= image_tag('My Web Site Logo.png') %> 

Note. Be sure to update YOUR HOST URL and My Website Logo.png in the above code.

For details on using Action Mailer, see ActionMailer :: Base .

+11
Feb 02 '16 at 7:34
source share

Copy inserted here

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

Embedded Attachments

You can also specify that the file should be displayed in line with other HTML. This is useful if you want to display a corporate logo or 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 

Then, to refer to the image in the view, you create a welcome.html.erb file and make an image_tag call in the attachment you want to display, and then call the url on the attachment to get the relative content path for the image source:

  <h1>Please Don't Cringe</h1> <%= image_tag attachments['photo.png'].url -%> 

Since we use the Action View image_tag method, you can pass any other parameters you want:

  <h1>Please Don't Cringe</h1> <%= image_tag attachments['photo.png'].url, alt: 'Our Photo', class: 'photo' -%> 
+4
Jun 23 '16 at 21:09
source share

I don’t know much about rails, but I worked on C # projects that create emails and then paste them into the users mailbox through the Google APIs. To create a letter, I had to generate a string from scratch. If you enable multipart for email, then image bytes will be included in the multi-page section using base64 encoding.

You can check the TMail and RubyMail packages to see if they support these actions for you.

0
Feb 07 2018-11-11T00:
source share



All Articles