From 15f962eec0ca121ba8bff4b91e355fb7b0dbc932 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 8 Aug 2011 07:29:43 -0400 Subject: [PATCH] directory dumping version --- cookbook/014/05-with-dir-dump-attachment.rb | 31 +++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 cookbook/014/05-with-dir-dump-attachment.rb diff --git a/cookbook/014/05-with-dir-dump-attachment.rb b/cookbook/014/05-with-dir-dump-attachment.rb new file mode 100644 index 0000000..963ab25 --- /dev/null +++ b/cookbook/014/05-with-dir-dump-attachment.rb @@ -0,0 +1,31 @@ +require 'rubygems' +require 'action_mailer' +require 'mime/types' + + +class SimpleMailer < ActionMailer::Base + def directory_dump_message(recipient, directory) + from 'directory-dump@localhost' + recipients recipient + subject "Dump of #{directory}" + body %{Here are the files currently in "#{directory}":} + + Dir.new(directory).each do |f| + path = File.join(directory, f) + if File.file? path + mime_type = MIME::Types.of(f).first + content_type = (mime_type ? mime_type.content_type : + 'application/octet-stream') + attachments[f] = { + :mime_type => mime_type, + :encoding => ('quoted-printable' if content_type =~ /^text\//), + :content => File.read(path) + } + end + end + end +end + + +puts SimpleMailer.directory_dump_message('daniel.buch+rubytest@gmail.com', + '/tmp')