lib/eco-rake/utils/mailer.rb in eco-rake-0.2.4 vs lib/eco-rake/utils/mailer.rb in eco-rake-0.2.5
- old
+ new
@@ -1,24 +1,36 @@
+require_relative 'mailer/provider_base'
+require_relative 'mailer/aws_provider'
+require_relative 'mailer/sendgrid_provider'
+
class EcoRake
module Utils
# Mailer helper that uses `aws-sdk-ses` **gem** (`Aws::SES::Client`)
# Several ENV vars should be configured for it to work (i.e. in the `.env` file):
# 1. `MAILER_REGION`
# 2. `MAILER_KEY_ID`
# 3. `MAILER_ACCESS_KEY`
# 4. `MAILER_FROM`
class Mailer
+ DEFAULT_PROVIDER = :sendgrid
+
+ attr_reader :provider
+ def initialize(provider: DEFAULT_PROVIDER)
+ @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
# @param from [String] the email address this is send **from**
def mail(to:, subject:, body:, from: nil, cc: nil, bcc: nil)
unless configured?
puts "Mailer: You are missing configuration parameters. Review your .env file"
return false
end
+
ses.send_email(
destination: fetch_to(to: to, cc: cc, bcc: bcc),
source: fetch_from(from),
message: {
subject: {
@@ -35,57 +47,59 @@
}
}
).tap do |response|
puts "Sent email to #{to} (MessageId: #{response.message_id})"
end
+ rescue StandardError => err
+ msg = "The mailer generated an exception:\n"
+ msg << err.patch_full_message(trace_count: 15)
+ puts msg
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)
- # @return [Boolean] whether or not the mailer is configured for usage.
- def configured?
- fetch_access_key_id && fetch_secret_access_key && fetch_region
- end
+ unless serv.configured?
+ msg = "Mailer: You are missing configuration parameters "
+ msg << "for '#{provider}'. Review your .env file"
+ puts msg
+ return false
+ end
- private
+ serv.send_mail(
+ subject: subject,
+ body: body,
+ to: to,
+ cc: cc,
+ bcc: bcc
+ ).tap do |response|
+ next unless response
- 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 => e
- puts "Trying to send an email with wrong email configuration: #{e}"
+ to_addr = serv.fetch_to(to)
+ msg = "Sent email #{ProviderBase.to_desc(to: to_addr, cc: cc, bcc: bcc)}"
+ puts msg
end
- @ses
+ rescue StandardError => err
+ msg = "The mailer generated an exception:\n"
+ msg << err.patch_full_message(trace_count: 15)
+ puts msg
end
- def fetch_to(to: nil, cc: nil, bcc: nil)
- {
- to_addresses: [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
+ private
- def fetch_from(value = nil)
- value || ENV['MAILER_FROM']
- end
-
- def fetch_access_key_id
- ENV['MAILER_KEY_ID']
- end
-
- def fetch_secret_access_key
- ENV['MAILER_ACCESS_KEY']
- end
-
- def fetch_region
- ENV['MAILER_REGION']
+ def service
+ case provider
+ when :aws
+ AwsProvider.new
+ when :sendgrid
+ SendgridProvider.new
+ else
+ puts "Unknown mail provider '#{provider}'"
+ nil
+ end
end
end
end
end