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



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

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



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

def close_attachments(msg)
  return if msg[:file_attachments].nil? || msg[:file_attachments].empty?
  msg[:file_attachments].each(&:close)
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
68
69
70
# File 'lib/lazylead/exchange.rb', line 54

def send(opts)
  to = opts["to"] || opts[:to]
  to = [to] unless to.is_a? Array
  html = make_body(opts)
  msg = {
    subject: opts["subject"],
    body: html,
    body_type: "HTML",
    to_recipients: to
  }
  msg.update(cc_recipients: opts["cc"]) if opts.key? "cc"
  add_attachments(msg, opts)
  cli.send_message msg
  close_attachments msg
  @log.debug "Email was generated from #{opts} and send by #{__FILE__}. " \
             "Here is the body: #{html}"
end