# rubocop:disable Naming/MethodParameterName module Eco module API module Common module Session class Mailer class AwsProvider < ProviderBase def send_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 } } } ) end # @return [Boolean] whether or not the mailer is configured for usage. def configured? fetch_access_key_id && fetch_secret_access_key && fetch_region end private def credentials @credentials ||= { id: 'AWS_ACCESS_KEY_ID', key: 'AWS_SECRET_ACCESS_KEY' } end def ses require 'aws-sdk-ses' @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}" } end def fetch_destination(to: nil, cc: nil, bcc: nil) { to_addresses: [fetch_to(to)].flatten.compact.uniq }.tap do |dest| cc = [cc].flatten.compact.uniq bcc = [bcc].flatten.compact.uniq dest.merge!(cc_addresses: cc) unless cc.empty? dest.merge!(bcc_addresses: bcc) unless bcc.empty? end 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 end end # rubocop:enable Naming/MethodParameterName