class RequestLogAnalyzer::Mailer

Mail report to a specified emailaddress

Attributes

data[RW]
host[RW]
port[RW]
to[RW]

Public Class Methods

new(to, host = 'localhost', options = {}) click to toggle source

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

   # File lib/request_log_analyzer/mailer.rb
16 def initialize(to, host = 'localhost', options = {})
17   require 'net/smtp'
18   @to      = to
19   @host    = host
20 
21   @port    = 25
22   @options = options
23   @host, @port = host.split(':') if @host.include?(':')
24   @data    = []
25 end

Public Instance Methods

<<(string) click to toggle source
   # File lib/request_log_analyzer/mailer.rb
54 def <<(string)
55   data << string
56 end
mail() click to toggle source

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.

   # File lib/request_log_analyzer/mailer.rb
29     def mail
30       from          = @options[:from]        || 'contact@railsdoctors.com'
31       from_alias    = @options[:from_alias]  || 'Request-log-analyzer reporter'
32       to_alias      = @options[:to_alias]    || to
33       subject       = @options[:subject]     || "Request log analyzer report - generated on #{Time.now}"
34       content_type  = ''
35       content_type  = 'Content-Type: text/html; charset="ISO-8859-1";' if @data.map { |l| l.include?('html') }.include?(true)
36       msg = <<END_OF_MESSAGE
37 From: #{from_alias} <#{from}>
38 To: #{to_alias} <#{@to}>
39 Subject: #{subject}
40 #{content_type}
41 
42 #{@data.join("\n")}
43 END_OF_MESSAGE
44 
45       unless @options[:debug]
46         Net::SMTP.start(@host, @port) do |smtp|
47           smtp.send_message msg, from, to
48         end
49       end
50 
51       [msg, from, to]
52     end
puts(string) click to toggle source
   # File lib/request_log_analyzer/mailer.rb
58 def puts(string)
59   data << string
60 end