Class: EcoRake::Utils::Mailer
- Inherits:
-
Object
- Object
- EcoRake::Utils::Mailer
- Defined in:
- lib/eco-rake/utils/mailer.rb,
lib/eco-rake/utils/mailer/aws_provider.rb,
lib/eco-rake/utils/mailer/provider_base.rb,
lib/eco-rake/utils/mailer/sendgrid_provider.rb
Overview
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):
MAILER_REGION
MAILER_KEY_ID
MAILER_ACCESS_KEY
MAILER_FROM
Defined Under Namespace
Classes: AwsProvider, ProviderBase, SendgridProvider
Constant Summary collapse
- DEFAULT_PROVIDER =
:sendgrid
Instance Attribute Summary collapse
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
Instance Method Summary collapse
-
#initialize(provider: DEFAULT_PROVIDER) ⇒ Mailer
constructor
A new instance of Mailer.
-
#mail(subject:, body:, to: nil, cc: nil, bcc: nil) ⇒ Object
Sends an email.
Constructor Details
#initialize(provider: DEFAULT_PROVIDER) ⇒ Mailer
Returns a new instance of Mailer.
17 18 19 |
# File 'lib/eco-rake/utils/mailer.rb', line 17 def initialize(provider: DEFAULT_PROVIDER) @provider = provider || DEFAULT_PROVIDER end |
Instance Attribute Details
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
16 17 18 |
# File 'lib/eco-rake/utils/mailer.rb', line 16 def provider @provider end |
Instance Method Details
#mail(subject:, body:, to: nil, cc: nil, bcc: nil) ⇒ Object
Sends an email
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/eco-rake/utils/mailer.rb', line 26 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: { charset: "UTF-8", data: subject }, body: { # NOTEL: `html:` will let you send html instead # you can use both at once if you like text: { charset: "UTF-8", data: body } } } ).tap do |response| puts "Sent email to #{to} (MessageId: #{response.})" end rescue StandardError => err msg = "The mailer generated an exception:\n" msg << err.(trace_count: 15) puts msg end |