Class: Ruboty::Handlers::Sendmail

Inherits:
Base
  • Object
show all
Defined in:
lib/ruboty/handlers/sendmail.rb

Instance Method Summary collapse

Instance Method Details

#sendmail(message) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruboty/handlers/sendmail.rb', line 10

def sendmail(message)
  mail_config = YAML.load_file("#{ENV['HOME']}/.ruboty-sendmail.yml")

  address = trim_address(message[:address])
  body = message[:body]

  mail = Mail.new do |config|
    config.from    mail_config['from']
    config.to      address
    config.subject mail_config['subject'] || 'Send by ruboty-sendmail'
    config.body    body
  end

  method = mail_config['method'] || :smtp

  default_option = {
    address:              'localhost',
    port:                 25,
    domain:               'localhost.localdomain',
    authentication:       nil,
    user_name:            nil,
    password:             nil,
    ssl:                  nil,
    enable_starttls_auto: true,
    openssl_verify_mode:  nil
  }
  option = default_option.merge(symbolize_keys(mail_config['option']))

  mail.delivery_method(method, option)
  begin
    mail.deliver!
  rescue
    message.reply('Failed :-(')
  else
    message.reply('Success!')
  end
end