I needed to fix an older app today that was written with Rails 1.2.3. It wasn’t sending inline attachments in emails correctly. It was trying to use one of the many gems named something like inline attachment (which may not be suitable for use with older Rails versions), but gave up and did it manually. I decided that it was easier to hand-code the ActionMailer::Part parts of the email than to use a gem designed to make this easier. I think that the app broke when the correct gem was uninstalled and could not be reinstalled (because it was removed from wherever it was published).
class Mailer < ActionMailer::Base
def postcard
recipients 'you@yourdomain'
from 'me@mydomain.com'
subject 'my email to you'
image_path = "#{RAILS_ROOT}/public/images/something.jpg"
base = File.basename(image_path)
cid = "#{base}@mydomain.com"
part :content_type => 'multipart/related' do |pt|
pt.part :content_type=> 'text/html' do |html|
html.body = "
"
end
attachment :content_type => "image/jpg; name=#{base}" do |a|
a.headers["Content-ID"]="<#{cid}>"
a.body = File.read(image_path)
end
end
end
end
