class Eco::API::Common::Session::Mailer

Public Class Methods

new(enviro:) click to toggle source

@param enviro [Eco::API::Common::Session::Environment]

# File lib/eco/api/common/session/mailer.rb, line 10
def initialize (enviro:)
  raise "Required Environment object (enviro:). Given: #{enviro}" if enviro && !enviro.is_a?(Eco::API::Common::Session::Environment)
  @enviro = enviro
end

Public Instance Methods

mail(to: nil, subject:, body:) click to toggle source

Sends an email @param to [String] destination email address @param subject [String] subject of the email @param body [String] `html` or plain text message

# File lib/eco/api/common/session/mailer.rb, line 19
def mail(to: nil, subject:, body:)
  ses.send_email(
    destination: {
      to_addresses: [fetch_to(to)].flatten,
    },
    source:  fetch_from,
    message: {
      subject: {
        charset: "UTF-8",
        data: subject,
      },
      body: {
        # NOTE - html: will let you send html instead
        # you can use both at once if you like
        text: {
          charset: "UTF-8",
          data: body
        }
      }
    }
  ).tap do |response|
    logger.debug("Sent email to #{to} (MessageId: #{response.message_id})")
  end
end

Private Instance Methods

config() click to toggle source
# File lib/eco/api/common/session/mailer.rb, line 63
def config
  @enviro.config || {}
end
fetch_access_key_id() click to toggle source
# File lib/eco/api/common/session/mailer.rb, line 75
def fetch_access_key_id
  config.mailer.access_key_id || ENV['AWS_ACCESS_KEY_ID']
end
fetch_from(value = nil) click to toggle source
# File lib/eco/api/common/session/mailer.rb, line 71
def fetch_from(value = nil)
  value || config.mailer.from
end
fetch_message_id_domain() click to toggle source
# File lib/eco/api/common/session/mailer.rb, line 91
def fetch_message_id_domain
  config.mailer.message_id_domain
end
fetch_region() click to toggle source
# File lib/eco/api/common/session/mailer.rb, line 83
def fetch_region
  config.mailer.region || ENV['AWS_REGION']
end
fetch_secret_access_key() click to toggle source
# File lib/eco/api/common/session/mailer.rb, line 79
def fetch_secret_access_key
  config.mailer.secret_access_key || ENV['AWS_SECRET_ACCESS_KEY']
end
fetch_server() click to toggle source
# File lib/eco/api/common/session/mailer.rb, line 87
def fetch_server
  config.mailer.server
end
fetch_to(value = nil) click to toggle source
# File lib/eco/api/common/session/mailer.rb, line 67
def fetch_to(value = nil)
  value || config.mailer.to
end
logger() click to toggle source
# File lib/eco/api/common/session/mailer.rb, line 59
def logger
  @enviro&.logger || ::Logger.new(IO::NULL)
end
ses() click to toggle source
# File lib/eco/api/common/session/mailer.rb, line 46
def ses
  begin
    @ses ||= Aws::SES::Client.new(
      access_key_id:     fetch_access_key_id,
      secret_access_key: fetch_secret_access_key,
      region:            fetch_region
    )
  rescue Exception => e
    logger.error("Trying to send an email with wrong email configuration: #{e}")
  end
  @ses
end