lib/eco/api/common/session/mailer.rb in eco-helpers-3.0.4 vs lib/eco/api/common/session/mailer.rb in eco-helpers-3.0.5

- old
+ new

@@ -1,43 +1,47 @@ +# rubocop:disable Naming/MethodParameterName module Eco module API module Common module Session class Mailer + include Eco::Language::AuxiliarLogger # @param enviro [Eco::API::Common::Session::Environment] - def initialize (enviro:) - raise "Required Environment object (enviro:). Given: #{enviro}" if enviro && !enviro.is_a?(Eco::API::Common::Session::Environment) + def initialize(enviro:) + msg = "Required Environment object (enviro:). Given: #{enviro.class}" + raise msg if enviro && !enviro.is_a?(Eco::API::Common::Session::Environment) + @enviro = enviro end # Sends an email # @param to [String] destination email address # @param subject [String] subject of the email # @param body [String] `html` or plain text message - def mail(to: nil, subject:, body:, cc: nil, bcc: nil) + def mail(subject:, body:, to: nil, cc: nil, bcc: nil) ses.send_email( destination: fetch_destination(to: to, cc: cc, bcc: bcc), - source: fetch_from, - message: { + source: fetch_from, + message: { subject: { charset: "UTF-8", - data: subject, + data: subject, }, - body: { - # NOTE - html: will let you send html instead + body: { + # NOTE: (html) will let you send html instead # you can use both at once if you like text: { charset: "UTF-8", - data: body + data: body } } } ).tap do |response| msg = "Sent email (MessageId: #{response.message_id}) to #{fetch_destination(to: to, cc: cc, bcc: bcc)}" puts msg - logger.debug(msg) + log(:debug) { msg } end end private @@ -47,18 +51,21 @@ @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}") + rescue StandardError => err + log(:error) { + "Trying to send an email with wrong email configuration: #{err}" + } end + @ses end def logger - @enviro&.logger || ::Logger.new(IO::NULL) + @enviro&.logger || super end def config @enviro.config || {} end @@ -102,5 +109,7 @@ end end end end end + +# rubocop:enable Naming/MethodParameterName