Embedding Dynamically Generated Images in Emails with Actionmailer

December 2nd, 2009, by Aaron

We recently had to embed images into our emails that were being sent with Actionmailer, and as such we turned to the inline_attachment plugin to achieve this. It very easily parses your mail output and overrides the ActionView path_to_image helper to attach the file and create an appropriate path to the attached image inside the email while splitting your mail into appropriate parts as necessary.

What we needed to do though was embed a dynamically generated image into the email. The image didn’t exist on the file system previously so we couldn’t use the standard image_tag helper that inline_attachment was patching.

So we extended ActionView to include a new tag helper attach_image_file that uses the existing inline_attachment part management and just a properly referenced image. The methods from inline_attachment take care of attaching the file to the email and splitting the mail content into the appropriate parts.

Here’s my code. I just added it into my initializers directory :

module ActionView
  module Helpers
    module AssetTagHelper
      def attach_image_file(file)
        @part_container ||= @controller
        if @part_container.is_a?(ActionMailer::Base) or @part_container.is_a?(ActionMailer::Part)
          basename  = "barcode.gif"
          ext       = basename.split('.').last
          cid       = Time.now.to_f.to_s + "#{basename}@inline_attachment"

          @part_container.inline_attachment(:content_type => "image/#{ext}",
                                        :body         => file,
                                        :filename     => basename,
                                        :cid          => "<#{cid}>",
                                        :disposition  => "inline")

          return ""
        end
      end
    end
  end
end

It did what we need, and allows us to properly inline a dynamic image. You could use similar code to inline pretty much anything that your target mail reader supports. Apple Mail for instance may provide PDF previews.

We are a Perth web design and web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. Jump to the Ruby on Rails category or contact us.

Leave a Reply


Follow Us

Stay in the Loop

  • Enter your email address to subscribe to our mailing list. You'll get updates about our products, specials and bonus offers, and general behind the scenes news from our team.

Twitter

Facebook Fans

Newsletters

Alexa Rank

Testimonial

The boys at The Frontier Group are amazing! For such a relaxed and personable organisation, they have phenomenal technical ability and a rampant professionalism. They have customisable solutions for all of my IT needs and they always deliver, on time and beyond expectation.

They fix problems other service providers can't and they helped me get a critical section of my web site up and running 10 minutes after I emailed the request!

Alex Hyndman, Nexus Car Share.

Featured Project

Case Study - Caudo Group - www.caudo.com.au

Website

www.caudo.com.au

Caudo Machinery

Caudo Group engaged our services to redesign their outdated website. We sent our photographer on-site to capture the essence of their business and turned it into a stunning web design.