Inline Attachments for ActionMailer

Mon Jun 07 23:40:00 -0700 2010

ActionMailer’s support for inline attachments sucks. Totally. Until now.

If you have ever tried to get an inline attachment working in Rails 2.x or even Rails 3 more recently, you were faced with an uphill battle that really did not deserve to be there.

For those not in the know, an Inline attachment in an email is simply an attachment that has a Content-Disposition of “inline” and allows you to reference it by using an HTML image tag with its source attribute pointing to cid:conent-id-of-image-part.

This allows you to put logos in your emails, header images etc. All embedded within the email itself.

So enough talk, this is how you do it, say you have a welcome message in your Action Mailer class, and in this welcome message you want to embed your company logo, which is conveniently located in your Rails root “public/images” directory.

First in your mailer, you would do:

class Notifier < ActionMailer::Base

  def welcome
    data = File.read(Rails.root.join('public/images/logo.png'))
    attachments.inline['logo.png'] = data
    mail
  end
  
end

Notice the inline call to attachments? What this does is tells Mail to mark this file with a content disposition of “inline” instead of the usual “attachment”.

Then in your view you would do:

<h1>Thank you for choosing ErnCorp!</h1>
<p><%= image_tag attachments['logo.png'].url -%></p>

Here, we are just interrogating the mail.attachments hash for the attachment with the filename “logo.png” and then calling url on the attachment we fine. This generates a standard email content ID resource locator (cid tag) that looks something like: cid:4c0da20e13de6@mikel.local.mail

And that is it!

Now the cool thing about this is that when you send this email, ActionMailer looks through your attachments list finding any inline attachments, if it finds some, then it changes your email to a multipart/related content type and then makes these attachments available to the view through the attachments helper.

Anyway, this is now pushed. Enjoy and have fun!

blogLater

Mikel

  1. Hawaii SEO Says:

    There are numerous resources designed to help the work from the consumer, however, ​​ resources created are usually in line with the wants of shoppers. Equipment definitely very useful.

  2. Hawaii SEO Says:

    There are numerous resources designed to help the work from the consumer, however, ​​ resources created are usually in line with the wants of shoppers. Equipment definitely very useful.

  3. shnwdct Says:

    I have a problem that is driving me nuts: When I use this, the attachment is the first part of the multipart mail, wreaking havoc in the mail clients.

  4. Dave Says:

    I’m still on rails 2.2.2

    any way I can use these new features? or does your checkin depend on rails 3 code?

  5. Mikel Lindsaar Says:

    @Dave

    Sorry mate, only Rails 3 :)

    The changes are too large to ActionMailer itself.

    However, you can use the mail gem in Rails 2 and just call #deliver on the mail object. Many people are doing this in Rails 2 at the moment.

    Mikel

  6. property valuer perth Says:

    They will have limit for the size to be attached. So they must make sure their datas can be sent.

  7. jerseysrainbow Says:

    This is all very new to me and this article really opened my eyes.Thanks for sharing with us .

  8. Chris Says:

    I have a problem that is driving me nuts: When I use this, the attachment is the first part of the multipart mail, wreaking havoc in the mail clients.

    Here is the code I use:

    class Mailer < ActionMailer::Base

    def mailing(recipient, subject, html_body, plain_body) attachments.inline[‘company_logo.jpg’] = File.read(…) mail(:to => recipient, :subject => subject) do |format| format.text { render :text=>plain_body } format.html { render :text=>html_body } end end

    end

    Do you have any suggestions?

  9. cool games Says:

    Nowadays,films has been a trade. Movie market desires lots of individuals. They need guys for the celebs and also fro the crews of the movie.

  10. best live web cam shows Says:

    At the momentmovies has been a business. Film industry wishes many individuals. They want guys for the stars and in addition fro the crews of the movie.

  11. Bo Says:

    The feature attachment helps people in sending file. The files which usually documents become easier to send. It does not have limit in the capacity.

  12. ZeekRewards Says:

    In this era people can send file without difficulty inside the web. The reason for this is that there’s attachment characteristic. This may have people to send any file to other folks in digital form.

  13. dfg4ydfg Says:

    I have a problem shingles symptoms that is driving me nuts: When I use this, the attachment is the first shingles vaccine part of the multipart mail, wreaking havoc in the mail clients.

  14. Fishing Lodges Alaska Says:

    Every mail’s users will need it for their activities. And all of email server have the functions for it.

  15. Movers in Virginia beach Says:

    This application seems cool and I make it easy to find out what the important news in my email. I already download and apply this software on my computer and I am quite happy to use it.

  16. syifa Says:

    Attachments help people a lot in effectively send and receive mail, with some more deeper and detail information to bring about. That’s quite helpful to find out this new method to attach files in.

  17. Engagement Rings Diamond Says:

    Most of websites has adding a captcha for avoiding them.They just will filling the database with unused things.

  18. Greg Benedict Says:

    That’s much easier than I expected. Thanks.

  19. custom home builders maryland Says:

    There is ample ways you can do to get a lot of lessons or tutorials. You can see a variety believed of sources to gain knowledge. Yo can fins in internet or books.

Leave a Reply