Class: Aws::ActionMailer::SESV2::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/action_mailer/ses_v2/mailer.rb

Overview

Provides a delivery method for ActionMailer that uses Amazon Simple Email Service V2.

Once you have a delivery method, you can configure your Rails environment to use it:

config.action_mailer.delivery_method = :ses_v2
config.action_mailer.ses_v2_settings = { region: 'us-west-2' }

You may also pass a preconstructed client:

sesv2_client = Aws::SESV2::Client.new(region: 'us-west-2')
config.action_mailer.ses_v2_settings = { sesv2_client: sesv2_client }

SendEmail Options

Settings in SEND_EMAIL_KEYS are forwarded directly to the SendEmail API call rather than to the client constructor. They can be configured at any level:

# Global (all emails)
config.action_mailer.ses_v2_settings = {
  region: 'us-west-2',
  configuration_set_name: 'Production'
}

# Per-mailer class
class MarketingMailer < ApplicationMailer
  default delivery_method_options: {
    configuration_set_name: 'Marketing',
    list_management_options: { contact_list_name: 'Promos', topic_name: 'Weekly' }
  }
end

Constant Summary collapse

SEND_EMAIL_KEYS =
%i[
  configuration_set_name
  email_tags
  list_management_options
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Mailer

Returns a new instance of Mailer.

Parameters:

  • settings (Hash) (defaults to: {})

    Passes along initialization settings to [Aws::SESV2::Client.new](docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/SESV2/Client.html#initialize-instance_method). You may pass ‘:sesv2_client` with a preconstructed SESV2::Client to reuse an existing instance (e.g. to avoid credential resolution on every delivery). When provided, the injected client is used and all other options are ignored.

    The following keys are extracted from settings and forwarded as parameters to the [SendEmail](docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/SESV2/Client.html#send_email-instance_method) API call (they are not passed to the client constructor):

    • ‘:configuration_set_name` - The name of the configuration set to use for this message.

    • ‘:email_tags` - A list of message tags (Types::MessageTag hashes).

    • ‘:list_management_options` - A Types::ListManagementOptions hash for SES subscription management.



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/aws/action_mailer/ses_v2/mailer.rb', line 66

def initialize(settings = {})
  @settings = settings
  @send_email_params = {}
  client_settings = settings.dup
  SEND_EMAIL_KEYS.each do |key|
    value = client_settings.delete(key)
    @send_email_params[key] = value if value
  end
  @client = client_settings.delete(:sesv2_client) || Aws::SESV2::Client.new(client_settings)

  update_user_agent
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



49
50
51
# File 'lib/aws/action_mailer/ses_v2/mailer.rb', line 49

def settings
  @settings
end

Instance Method Details

#deliver!(message) ⇒ Object

Delivers a Mail::Message object. Called during mail delivery.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/aws/action_mailer/ses_v2/mailer.rb', line 80

def deliver!(message) # rubocop:disable Metrics/MethodLength
  params = { content: { raw: { data: message.to_s } } }
  params[:from_email_address] = from_email_address(message)
  params[:destination] = {
    to_addresses: to_addresses(message),
    cc_addresses: message.cc,
    bcc_addresses: message.bcc
  }
  params.merge!(@send_email_params)

  @client.send_email(params).tap do |response|
    message.header[:ses_message_id] = response.message_id
  end
end