Rails - use the same application for all emails using the layout

I'm probably missing something obvious, but I have a logo that I would like to include in all the emails that I send from my application. I have a layout that I use for all of these Mailers. I suppose there is a way to do this DRY and not add a line of code to attach the file to each method of the mailer. Can someone point me in the right direction or correct my thought.

Thanks!

+7
source share
4 answers

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

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

Since they will be implemented using AbstractController :: Callbacks, you can do the following to simulate the functionality that will be present in ActionMailer :: Base when 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 a module that will be used in a future version of the rails, so the callbacks available to you will be the same for future compatibility. The code will rise when you try to upgrade to a version of Rails that already includes AbstractController :: Callbacks, so you will be reminded of the removal of conditional logic.

+7
source

I hacked a bit, this is not perfect, 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, since set_layout receives the call to set the header, it also adds inline attachments. It basically creates a callback to add attachments.

It prefers a real callback system in ActionMailer , but it works too.

I think that I will share since I was looking for this answer on this issue today.

+2
source

in the layout file that your mail program uses, you can add the following

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

I assume the mail sent is html or multipart.

You will also need to make changes to the environment files. ActionMailer does not get base_url by default. For example, in /development.rb environments, I added the following

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

If you want to do it in a dRY style (and as an attachment), perhaps you could do something like

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

I know that you asked about attaching inline images, but here is a different approach that allows you to achieve the same element with less complexity.

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

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

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

I use the base64 encoder / decoder online tool at http://www.base64-image.net to create the full <img />

This approach has several advantages:
- there is no attachment code that makes the backend server code cleaner and easier to read
- no increase in email size - embedded image attachments are converted to base64 anyway, so this approach does not make the mail payload larger
- it is widely supported - if the receiving mail client shows html , it is quite likely that it also supports this method

-one
source

All Articles