Rails - Use same attachment for all emails using layout

风格不统一 提交于 2019-12-22 05:06:47

问题


I'm probably missing something obvious, but I've got a logo I'd like to include in all of the emails I send from my app. I have a master layout I'm using for all of those Mailers. I assume there's a way to do keep it DRY and not have to add the line of code to attach the file in every mailer method. Can someone point me in the right direction or correct my line of thought.

Thanks!


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/5113121/rails-use-same-attachment-for-all-emails-using-layout

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