class Mail::SES

Mail delivery method handler for AWS SES

Constants

RAW_EMAIL_ATTRS
VERSION

Attributes

client[R]
settings[RW]

Public Class Methods

build_client_options(options) click to toggle source
# File lib/mail/ses.rb, line 69
def build_client_options(options)
  options[:credentials] = Aws::InstanceProfileCredentials.new if options.delete(:use_iam_profile)
  options
end
build_raw_email_options(message, options = {}) click to toggle source
# File lib/mail/ses.rb, line 74
def build_raw_email_options(message, options = {})
  output = slice_hash(options, *RAW_EMAIL_ATTRS)
  output[:source] ||= message.from.first
  output[:destinations] = [message.to, message.cc, message.bcc].flatten.compact
  output[:raw_message] = { data: message.to_s }
  output
end
new(options = {}) click to toggle source

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::SES::Client.
# File lib/mail/ses.rb, line 24
def initialize(options = {})
  @mail_options = options.delete(:mail_options) || {}
  @error_handler = options.delete(:error_handler)
  @settings = { return_response: options.delete(:return_response) }
  self.class.validate_error_handler(@error_handler)
  options = self.class.build_client_options(options)
  @client = Aws::SES::Client.new(options)
end
validate_error_handler(error_handler) click to toggle source
# File lib/mail/ses.rb, line 53
def validate_error_handler(error_handler)
  raise ArgumentError.new(':error_handler must be a Proc') if error_handler && !error_handler.is_a?(Proc)
end
validate_mail(mail) click to toggle source
# File lib/mail/ses.rb, line 57
def validate_mail(mail)
  unless mail.is_a?(Mail::Message)
    raise ArgumentError.new('mail must be an instance of Mail::Message class')
  end

  Mail::CheckDeliveryParams.check(mail)

  if mail.has_attachments? && mail.text_part.nil? && mail.html_part.nil?
    raise ArgumentError.new('Attachment provided without message body')
  end
end

Protected Class Methods

slice_hash(hash, *keys) click to toggle source
# File lib/mail/ses.rb, line 84
def slice_hash(hash, *keys)
  keys.each_with_object({}) { |k, h| h[k] = hash[k] if hash.key?(k) }
end

Public Instance Methods

deliver!(mail, options = {}) click to toggle source

Delivers a Mail object via SES.

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

in the initializer (optional, default: {}). Refer to
Aws::SES::Client#send_raw_email
# File lib/mail/ses.rb, line 39
def deliver!(mail, options = {})
  self.class.validate_mail(mail)
  options = @mail_options.merge(options || {})
  raw_email_options = self.class.build_raw_email_options(mail, options)
  begin
    response = client.send_raw_email(raw_email_options)
    mail.message_id = "#{response.to_h[:message_id]}@email.amazonses.com"
    settings[:return_response] ? response : self
  rescue StandardError => e
    @error_handler ? @error_handler.call(e, raw_email_options.dup) : raise(e)
  end
end