lib/backup/notifier/mail.rb in backup-3.0.20 vs lib/backup/notifier/mail.rb in backup-3.0.21
- old
+ new
@@ -7,14 +7,10 @@
module Backup
module Notifier
class Mail < Base
##
- # Container for the Mail object
- attr_reader :mail
-
- ##
# Mail delivery method to be used by the Mail gem.
# Supported methods:
#
# `:smtp` [::Mail::SMTP] (default)
# : Settings used only by this method:
@@ -92,19 +88,17 @@
# Folder where mail will be kept when using the `:file` `delivery_method` option.
# Default location is '$HOME/Backup/emails'
# Example: '/tmp/test-mails'
attr_accessor :mail_folder
- ##
- # Performs the notification
- # Extends from super class. Must call super(model, exception).
- # If any pre-configuration needs to be done, put it above the super(model, exception)
- def perform!(model, exception = false)
- super(model, exception)
+ def initialize(model, &block)
+ super(model)
+
+ instance_eval(&block) if block_given?
end
- private
+ private
##
# Notify the user of the backup operation results.
# `status` indicates one of the following:
#
@@ -128,31 +122,35 @@
case status
when :success then [ 'Success', false ]
when :warning then [ 'Warning', true ]
when :failure then [ 'Failure', true ]
end
- mail.subject = "[Backup::%s] #{model.label} (#{model.trigger})" % name
- mail.body = template.result('notifier/mail/%s.erb' % status.to_s)
+
+ email = new_email
+ email.subject = "[Backup::%s] #{@model.label} (#{@model.trigger})" % name
+ email.body = @template.result('notifier/mail/%s.erb' % status.to_s)
+
if send_log
- mail.convert_to_multipart
- mail.attachments["#{model.time}.#{model.trigger}.log"] = {
+ email.convert_to_multipart
+ email.attachments["#{@model.time}.#{@model.trigger}.log"] = {
:mime_type => 'text/plain;',
:content => Logger.messages.join("\n")
}
end
- mail.deliver!
+
+ email.deliver!
end
##
# Configures the Mail gem by setting the defaults.
- # Instantiates the @mail object with the @to and @from attributes
- def set_defaults!
- @delivery_method = %w{ smtp sendmail file test }.
+ # Creates and returns a new email, based on the @delivery_method used.
+ def new_email
+ method = %w{ smtp sendmail file test }.
index(@delivery_method.to_s) ? @delivery_method.to_s : 'smtp'
options =
- case @delivery_method
+ case method
when 'smtp'
{ :address => @address,
:port => @port,
:domain => @domain,
:user_name => @user_name,
@@ -164,22 +162,22 @@
opts = {}
opts.merge!(:location => File.expand_path(@sendmail)) if @sendmail
opts.merge!(:arguments => @sendmail_args) if @sendmail_args
opts
when 'file'
- @mail_folder ||= "#{ENV['HOME']}/Backup/emails"
+ @mail_folder ||= File.join(Config.root_path, 'emails')
{ :location => File.expand_path(@mail_folder) }
when 'test' then {}
end
- method = @delivery_method.to_sym
::Mail.defaults do
- delivery_method method, options
+ delivery_method method.to_sym, options
end
- @mail = ::Mail.new
- @mail.from = @from
- @mail.to = @to
+ email = ::Mail.new
+ email.to = @to
+ email.from = @from
+ email
end
end
end
end