Rails - Use same attachment for all emails using layout

徘徊边缘 提交于 2019-12-05 05:19:06

Callbacks using before_filter and after_filter will be supported in a future Rails release:

http://github.com/rails/rails/commit/4f28c4fc9a51bbab76d5dcde033c47aa6711339b

Since they will be implemented using AbstractController::Callbacks, you can do the following to mimic the functionality that will be present in ActionMailer::Base once Rails 4 is released:

class YourMailer < ActionMailer::Base
  if self.included_modules.include?(AbstractController::Callbacks)
    raise "You've already included AbstractController::Callbacks, remove this line."
  else
    include AbstractController::Callbacks
  end

  before_filter :add_inline_attachments!

  private
  def add_inline_attachments!
    attachments.inline["footer.jpg"] = File.read('/path/to/filename.jpg')
  end
end

This includes the module that will be used in a future rails version, so the callback hooks available to you will be the same to ensure future compatibility. The code will raise when you try to upgrade to a Rails version that already includes AbstractController::Callbacks, so you will be reminded to remove the conditional logic.

I hacked a little something, it's not ideal, but it works.

If you use

default "SOMEHEADER", Proc.new { set_layout }

And then define set_layout

def set_layout
  attachments.inline["logo.png"] = File.read("logopath.png")
  attachments.inline["footer.jpg"] = File.read("footerpath.png")
  "SOME HEADER VALUE"
end

Then because set_layout gets called to set the header, it also adds the inline attachments. It basically creates a callback for adding attachments.

An actual callback system in ActionMailer would be preferable, but this works too.

Thought I would share since I was looking for this answer on this question earlier today.

in the layout file that your mailer uses u can add the following

<%= image_tag('logo.png') %>

I am assuming that the mail being sent out is html or multipart.

Also you will need to make changes in the environment files. ActionMailer does not get a default base_url. For e.g in environments/development.rb I added the following

config.action_mailer.default_url_options = { :host => "localhost:3000" }

If you want to do it in a dRY manner (and as an attachment) maybe you could do something like

class MyMailer < ActionMailer::Base
  default :attachment => File.read(File.join(Rails.root,'public','images','logo.png'))
end

I know you've asked about attaching inline images, but here's a different approach that achieves the same thing with less complexity..

Using inline base64 encoded images in the html layout - no attachments required!

Basically just change the src="..." of your logo image to the format:

<img alt="example logo"
   width="32px"
  height="32px"
     src="data:image/png;base64,iVBORw0KGgoAAA....."/>

I use the online base64 encoder / decoder tool at http://www.base64-image.net for generating the complete <img /> tag

This approach has a few benefits:
- no attachment code, which makes the backend server code cleaner and easier to read
- no increase in email size - inline image attachments are converted to base64 anyway so this approach doesn't make the email payload any larger
- it's widely supported - if the receiving email client is showing html, it's pretty likely it also supports this method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!