# rubocop:disable Naming/MethodParameterName require_relative 'mailer/provider_base' require_relative 'mailer/aws_provider' require_relative 'mailer/sendgrid_provider' module Eco module API module Common module Session class Mailer include Eco::Language::AuxiliarLogger DEFAULT_PROVIDER = :sendgrid attr_reader :provider # @param enviro [Eco::API::Common::Session::Environment] def initialize(enviro:, provider: DEFAULT_PROVIDER) msg = "Required Environment object (enviro:). Given: #{enviro.class}" raise msg if enviro && !enviro.is_a?(Eco::API::Common::Session::Environment) @enviro = enviro @provider = provider || DEFAULT_PROVIDER 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(subject:, body:, to: nil, cc: nil, bcc: nil) return false unless (serv = service) unless serv.configured? msg = "Mailer: You are missing configuration parameters " msg << "for '#{provider}'. Review your .env file" log(:error) { msg } return false end serv.send_mail( subject: subject, body: body, to: to, cc: cc, bcc: bcc ).tap do |response| next unless response to_addr = serv.fetch_to(to) # msg = "Sent email (MessageId: #{response.message_id}) to #{fetch_destination(to: to, cc: cc, bcc: bcc)}" msg = "Sent email #{ProviderBase.to_desc(to: to_addr, cc: cc, bcc: bcc)}" puts msg log(:debug) { msg } end end private def service case provider when :aws AwsProvider.new(config, logger: logger) when :sendgrid SendgridProvider.new(config, logger: logger) else msg = "Unknown mail provider '#{provider}'" puts msg log(:debug) { msg } nil end end def logger @enviro&.logger || super end def config @enviro.config || {} end end end end end end # rubocop:enable Naming/MethodParameterName