module RequestLogAnalyzer
# Mail report to a specified emailaddress
class Mailer
attr_accessor :data, :to, :host, :content_type
# Initialize a mailer
# to to email address to mail to
# host the mailer host (defaults to localhost)
# options Specific style options
#
# Options
# * :debug Do not actually mail
# * :from_alias The from alias
# * :to_alias The to alias
# * :subject The message subject
def initialize(to, host = 'localhost', options = {})
require 'net/smtp'
@to = to
@host = host
@options = options
@data = []
@content_type = nil
end
# Send all data in @data to the email address used during initialization.
# Returns array containg [message_data, from_email_address, to_email_address] of sent email.
def mail
from = @options[:from] || 'contact@railsdoctors.com'
from_alias = @options[:from_alias] || 'Request-log-analyzer reporter'
to_alias = @options[:to_alias] || to
subject = @options[:subject] || "Request log analyzer report - generated on #{Time.now.to_s}"
content_type= "Content-Type: #{@content_type}" if @content_type
msg = <
To: #{to_alias} <#{@to}>
Subject: #{subject}
#{content_type}
#{@data.to_s}
END_OF_MESSAGE
unless @options[:debug]
Net::SMTP.start(@host) do |smtp|
smtp.send_message msg, from, to
end
end
return [msg, from, to]
end
def << string
data << string
end
def puts string
data << string
end
end
end