I’ve just rewritten inline_attachment to have more features, with less code, and be more reliable (ie. not as likely to break with a Rails version bump).
The coolest new feature is full support for the ActionMailer auto-multipart feature, so mostly you won’t need to write any complex MIME part crap in your model. No code is always good :)
Quickly
app/models/notifier.rb
class Notifier < ActionMailer::Base
def signup
recipients %q{"Testing IA" <testing@handle.it>}
from %q{"Mr Tester" <tester@handle.it>}
subject "Here's a funky test"
end
end
# Oh yeah baby! Read it and weep! So how's this work? Well, you'll need
# your templates named properly - see the `Multipart email` section of the
# ActionMailer::Base docs.
signup.text.plain.erb
Your username is: <%= @username %>
signup.text.html.erb
<html>
<head>
<title>Signup Notification</title>
</head>
<body>
<%= image_tag "logo.png" %>
<p>Your username is: <%=h @username %>
</body>
</html>
Obviously you also need that logo.png in your public/images directory.
Although that’s sexy, the biggest improvement is actually that I rewrite Edmond’s stuff so that instead of having to copy-paste the imagetag method in order to sprinkle in the inline code, I’ve chained a method in front of pathto_image. The only downside of this is that the :alt parameter is now set to the “cid:” value by default (instead of the root of the filename) but get over it and just supply an alt value if you want one.
This improvement means that the gem is less likely to break with new Rails versions, because it doesn’t have to keep up with the image_tag method.


Comments
There are 18 comments on this post. Post yours →
I’m using the inline_attachment gem and almost have everything working well now, except that what works in development does not work in production.
when sent from the development machine here’s what gets inserted by image_tag in the email that goes out:
when sent from production:
Note the relative path.
So, something’s not getting resolved correctly in production. Can anyone pt me in the right direction to figure out why?
if it matters, I’m using actionmailer 2.2.2.
thx
hmmm… the above is missing what gets inserted in the outbound emails…
from dev, the src part of the img tag:
src=”cid: 1237146569.011RedeyeLogo.gif@inline_attachment”
from prod, the src part of the img tag:
src=”/images/RedeyeLogo.gif?1236723122”
I also note that the Content-ID is missing from the header
That looks like you don’t have inline_attachment installed in your production environment. Did you remember to install the gem in production (or install the gem into your vendor/gems dir)?
Yeah, that’s what I thought for awhile, but here’s what a ‘gems list -d” gives (for your gem):
inline_attachment (0.4.0) Author: Jason King Rubyforge: http://rubyforge.org/projects/InlineAttachment Homepage: http://github.com/JasonKing/inline_attachment Installed at: /usr/lib/ruby/gems/1.8
So, it ~seems~ like it’s installed glabally in the right place, right? Or does it need to be installed in the app’s vendor/gems dir?
Also, the “config.gem ‘inline_attachment’” line in environment.rb isnt choking…
No, that should work.
So do you have the same version of Rails (and I guess I should check - Ruby) in production as in dev?
yep 2.2.2 …but… I do also have 1.2.6 still there (in both cases) for legacy reasons for an older “legacy” app that is staged from the same server as the app in question
could that be a problem (though it doesnt appear to be on my dev machine)?
also, perhaps I should mention, that the mailer is being invoked from a rake task driven by a cron job (it’s invoked by the same rake task manually on the dev box)… again, any problem with that?
btw - thx for your responsiveness on this… and the work done on the gem in the first place
Shouldn’t be the problem, you should have a RAILSGEMVERSION const set in your config/environment.rb file which sets the version to 2.2.2.
Only problem with the rake task is that I’ve never done that and haven’t tested it. If it works in dev it should work in prod.
I’ll have a look at this some more and get back to you, maybe there’s something that I’m (not) doing meaning that it doesn’t get loaded for a rake task. Let me know if you work it out in the meantime.
And, you’re welcome for the help. You could consider recommending me if you want to pay me back in a currency I appreciate :) Link is on the right, the “wwrails” logo.
Ha, Markdown bit me. Above should say RAILS_GEM_VERSION
Sorry lunaclaire.
Everything ran here with zero problems with both 0.4.0 (from RubyForge) and the new 0.4.1 (from github), and in Rails 2.2.2 and 2.3.1 and 2.3.2 - whether in a web app, or in a rake task.
Basically I couldn’t make it fail.
I still think the most likely cause is that your app is running 1.x in production - that could definitely make it fail. I haven’t looked back at 1.2.6 but I totally changed the method that was overridden, and my gut feel is that that method wasn’t even in 1.2.6 so it would just fail silently.
thx for digging further
my main app is not using 1.2.6 (that’s for another legacy app) and I’m frozen to 2.2.2 right now
but perhaps because this code is invoked from the rake task, 1.2.6 comes in somehow…? I’ll play around a bit and see if I can make sure it’s not 1.2.6 getting used by the rake task
(btw - if you’d like to keep this thread uncluttered with our back and forth, I’d be glad to take it offline and only post the “solution” for others when it’s all straight… I think you have my email addrs from these postings)
Hello there… Wanted to share something I’ve been busy the whole afternoon tracking an “undefined method ‘template_format’” using inline_attachment.
see here below :
in app/models/notifier.rb
in app/views/notifier/signup.text.plain.erb
in app/views/notifier/signup.text.html.erb
welcomelogo.gif is present in public/images
in config/environment.rb
the code started to work as soon as I commented out the line :
require 'inline_attachment'in the environment.rbIf that can help
F.
[edit: fixed markup]
Jason, nice work. I’m trying to make it work as a plugin. Is there something I need to do to make it work despite script/plugin install git://github.com/JasonKing/inline_attachment.git?
@Marcelo - Making it into a plugin is something that logandk contributed just the other day, and I haven’t actually checked to make sure it works. There shouldn’t be any problem with loading as a plugin, there’s nothing complex that I do, but I’ve not had a close look yet.
What behaviour are you seeing that you’re not expecting?
Hi Jason, thanks for the neat plugin! First it really made me weep of joy. But when I tried to find a way of attaching ‘outline’ files it made me weep again for I couldn’t find any. Is there any kind of workaround or even an official way of sending both kinds of attachments?
Tom
Thanks! Your plugin is greatly appreciated.
@Niko - you’re welcome. Thanks for dropping by.
@Tom - I replied to the email that you sent too. I’ll catch up with you there.
sorry for the confusion. I just couldn’t find a way of attaching regular files like pdf documents, which shouldn’t appear “inline”, to an email while using your plugin. I could only send emails containing inline information (text, html) or having only files normally attached. Thus I’d like to know whether there is a way of using ‘inline_attachment’ and attach normal files as well.
ups - didn’t enter my name …
Post a comment
Required fields look like this.