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.
Tyrone Wilson Sep 12 '14 at 2:03 a.m. 2014-09-12 14:03
source share