lib/eco/api/common/session/mailer.rb in eco-helpers-3.0.14 vs lib/eco/api/common/session/mailer.rb in eco-helpers-3.0.15

- old
+ new

@@ -1,111 +1,82 @@ # 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:) + 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 + @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) - ses.send_email( - destination: fetch_destination(to: to, cc: cc, bcc: bcc), - 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 - } - } - } + 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| - msg = "Sent email (MessageId: #{response.message_id}) to #{fetch_destination(to: to, cc: cc, bcc: bcc)}" + 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 ses - require 'aws-sdk-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 StandardError => err - log(:error) { - "Trying to send an email with wrong email configuration: #{err}" - } + 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 - - @ses end def logger @enviro&.logger || super end def config @enviro.config || {} - end - - def fetch_destination(to: nil, cc: nil, bcc: nil) - cc_to = [cc].flatten.compact.uniq - bcc_to = [bcc].flatten.compact.uniq - { to_addresses: [fetch_to(to)].flatten }.tap do |dest| - dest.merge!(cc_addresses: cc_to) unless cc_to.empty? - dest.merge!(bcc_addresses: bcc_to) unless bcc_to.empty? - end - end - - def fetch_to(value = nil) - value || config.mailer.to - end - - def fetch_from(value = nil) - value || config.mailer.from - end - - def fetch_access_key_id - config.mailer.access_key_id || ENV['AWS_ACCESS_KEY_ID'] - end - - def fetch_secret_access_key - config.mailer.secret_access_key || ENV['AWS_SECRET_ACCESS_KEY'] - end - - def fetch_region - config.mailer.region || ENV['AWS_REGION'] - end - - def fetch_server - config.mailer.server - end - - def fetch_message_id_domain - config.mailer.message_id_domain end end end end end