class Ruboty::Handlers::Sendmail

Public Instance Methods

sendmail(message) click to toggle source
# 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

Private Instance Methods

symbolize_keys(hash) click to toggle source
# File lib/ruboty/handlers/sendmail.rb, line 58
def symbolize_keys(hash)
  hash.each_with_object({}) do |(k, v), retval|
    retval[k.to_sym] = v
  end
end
trim_address(string) click to toggle source
# File lib/ruboty/handlers/sendmail.rb, line 50
def trim_address(string)
  if match_data = string.match(/(?<address>[a-zA-Z0-9]+[\w\.-]*@[a-zA-Z0-9]+[\w\.-]+)/)
    match_data[:address]
  else
    nil
  end
end