Class: Lazylead::Exchange

Inherits:
Object
  • Object
show all
Includes:
Emailing
Defined in:
lib/lazylead/exchange.rb

Overview

TODO:

#/DEV Add support of symbols for options in order to use both notations like opts or opts.

A postman to send emails to the Microsoft Exchange server.

Author

Yurii Dubinka ([email protected])

Copyright

Copyright © 2019-2020 Yurii Dubinka

License

MIT

Instance Method Summary collapse

Methods included from Emailing

#make_body, #split

Constructor Details

#initialize(log = Log.new, salt = Salt.new("exchange_salt"), opts = ENV.to_h) ⇒ Exchange

Returns a new instance of Exchange.



44
45
46
47
48
49
50
# File 'lib/lazylead/exchange.rb', line 44

def initialize(
  log = Log.new, salt = Salt.new("exchange_salt"), opts = ENV.to_h
)
  @log = log
  @salt = salt
  @opts = opts
end

Instance Method Details

#add_attachments(msg, opts) ⇒ Object



78
79
80
81
82
# File 'lib/lazylead/exchange.rb', line 78

def add_attachments(msg, opts)
  return unless opts.key? "attachments"
  files = split("attachments", opts).map { |f| File.open(f, "r") }
  msg[:file_attachments] = files
end

#close_attachments(msg) ⇒ Object



84
85
86
87
# File 'lib/lazylead/exchange.rb', line 84

def close_attachments(msg)
  return if msg[:file_attachments].nil? || msg[:file_attachments].empty?
  msg[:file_attachments].each(&:close)
end

#make_msg(to, opts) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/lazylead/exchange.rb', line 69

def make_msg(to, opts)
  {
    subject: opts["subject"],
    body: make_body(opts),
    body_type: "HTML",
    to_recipients: to
  }
end

#send(opts) ⇒ Object

Send an email.

:opts

the mail configuration like from, cc, subject, template.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lazylead/exchange.rb', line 54

def send(opts)
  to = opts["to"] || opts[:to]
  to = [to] unless to.is_a? Array
  if to.reject { |e| e.nil? || e.blank? }.empty?
    @log.warn "Email can't be sent to '#{to}, more: '#{opts}'"
    return
  end
  msg = make_msg(to, opts)
  msg.update(cc_recipients: opts["cc"]) if opts.key? "cc"
  add_attachments(msg, opts)
  cli.send_message msg
  close_attachments msg
  @log.debug "#{__FILE__} sent '#{opts['subject']}' to '#{to}'."
end