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




Fri May 04 18:20:08 -0700 2012
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.
Fri May 04 18:20:42 -0700 2012
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.
Tue Nov 08 19:38:20 -0800 2011
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.
Thu Jun 24 16:54:55 -0700 2010
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?
Sat Jun 26 17:46:34 -0700 2010
@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
Sat May 12 09:42:57 -0700 2012
They will have limit for the size to be attached. So they must make sure their datas can be sent.
Wed Nov 16 18:10:36 -0800 2011
This is all very new to me and this article really opened my eyes.Thanks for sharing with us .
Thu Jul 14 02:14:53 -0700 2011
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 endend
Do you have any suggestions?
Fri Jan 13 02:46:58 -0800 2012
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.
Mon Jan 23 18:31:18 -0800 2012
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.
Thu Jan 26 23:17:35 -0800 2012
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.
Thu Feb 02 23:36:58 -0800 2012
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.
Sun Feb 05 20:25:53 -0800 2012
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.
Mon Feb 20 10:57:14 -0800 2012
Every mail’s users will need it for their activities. And all of email server have the functions for it.
Fri Mar 02 22:38:51 -0800 2012
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.
Mon Mar 05 02:00:49 -0800 2012
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.
Sun Mar 25 02:28:55 -0700 2012
Most of websites has adding a captcha for avoiding them.They just will filling the database with unused things.
Mon Apr 23 21:32:35 -0700 2012
That’s much easier than I expected. Thanks.
Mon Apr 30 01:13:15 -0700 2012
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.