Class: Mail::SES

Inherits:
Object
  • Object
show all
Defined in:
lib/mail/ses.rb,
lib/mail/ses/version.rb,
lib/mail/ses/options_builder.rb,
lib/mail/ses/message_validator.rb

Overview

Mail delivery method handler for AWS SES

Defined Under Namespace

Classes: MessageValidator, OptionsBuilder

Constant Summary collapse

VERSION =
'1.0.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SES

Initializes the Mail::SES object.

options - The Hash options (optional, default: {}):

:mail_options    - (Hash) Default AWS options to set on each mail object.
:error_handler   - (Proc<Error, Hash>) Handler for AWS API errors.
:use_iam_profile - Shortcut to use AWS IAM instance profile.
All other options are passed-thru to Aws::SESV2::Client.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mail/ses.rb', line 20

def initialize(options = {})
  @mail_options = options.delete(:mail_options) || {}

  @error_handler = options.delete(:error_handler)
  raise ArgumentError.new(':error_handler must be a Proc') if @error_handler && !@error_handler.is_a?(Proc)

  @settings = { return_response: options.delete(:return_response) }

  options[:credentials] = Aws::InstanceProfileCredentials.new if options.delete(:use_iam_profile)
  @client = Aws::SESV2::Client.new(options)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/mail/ses.rb', line 11

def client
  @client
end

#settingsObject

Returns the value of attribute settings.



10
11
12
# File 'lib/mail/ses.rb', line 10

def settings
  @settings
end

Instance Method Details

#deliver!(message, options = {}) ⇒ Object

Delivers a Mail::Message object via SES.

message - The Mail::Message object to deliver (required). options - The Hash options which override any defaults set in :mail_options

in the initializer (optional, default: {}). Refer to
Aws::SESV2::Client#send_email


38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mail/ses.rb', line 38

def deliver!(message, options = {})
  MessageValidator.new(message).validate

  options = @mail_options.merge(options || {})
  send_options = OptionsBuilder.new(message, options).build

  begin
    response = client.send_email(send_options)
    message.message_id = "#{response.to_h[:message_id]}@email.amazonses.com"
    settings[:return_response] ? response : self
  rescue StandardError => e
    handle_error(e, send_options)
  end
end