Module: EBPS::Util::Mail

Defined in:
lib/ebps/util/mail.rb

Class Method Summary collapse

Class Method Details

.notify(log) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ebps/util/mail.rb', line 7

def Mail.notify log
  config = EBPS.config
  header, message = setup
  header.from = config.report_from
  to = header.to = config.report_to
  time = Time.now.strftime config.report_time_format
  header.subject = sprintf config.report_subject,
                           File.basename(config.target), time
  message.body = log.collect do |item|
    case item
    when StandardError
      header.subject = sprintf config.report_subject_err,
                               File.basename(config.target), time
      sprintf "%s: %s\n%s", item.class, item.message, item.backtrace.join("\n")
    else
      item.to_s
    end
  end.join("\n" * 2)
  sendmail message, to
end

.sendmail(message, to, cc = []) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/ebps/util/mail.rb', line 27

def Mail.sendmail(message, to, cc=[])
  config = EBPS.config
  Net::SMTP.start(config.smtp_server, config.smtp_port, config.smtp_domain,
                  config.smtp_user, config.smtp_pass, 
                  config.smtp_authtype) do |smtp|
    smtp.sendmail(message.to_s, config.smtp_user, [to, cc].flatten.compact)
  end
end

.setupObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/ebps/util/mail.rb', line 35

def Mail.setup
  message = RMail::Message.new
  header = message.header
  header.add('Date', Time.now.rfc822)
  header.add('Mime-Version', '1.0')
  header.add('User-Agent', EBPS.config.name)
  header.add('Content-Type', 'text/plain', nil, 'charset' => 'utf-8')
  header.add('Content-Disposition', 'inline')
  [header, message]
end