Repository

aws-actionmailbox-ses-ruby

Amazon Simple Email Service (SES) as an ActionMailbox Ingress

README

Amazon Simple Email Service (SES) as an ActionMailbox Ingress

Gem Version
Build Status
Github forks
Github stars

This gem contains an ActionMailbox
ingress using Amazon SES, SNS, and S3.

Installation

Add this gem to your Rails project's Gemfile:

gem 'aws-sdk-rails', '~> 5'
gem 'aws-actionmailbox-ses', '~> 0'

Then run bundle install.

This gem also brings in the following AWS gems:

  • aws-sdk-s3
  • aws-sdk-sns

You will have to ensure that you provide credentials for the SDK to use. See the
latest AWS SDK for Ruby Docs
for details.

If you're running your Rails application on Amazon EC2, the AWS SDK will
check Amazon EC2 instance metadata for credentials to load. Learn more:
IAM Roles for Amazon EC2

Configuration

Amazon SES/SNS

  1. Configure SES
    to save emails to S3
    or to send them as raw messages.

  2. Configure the SNS topic for SES or for the S3 action
    to send notifications to +/rails/action_mailbox/ses/inbound_emails+. For example,
    if your website is hosted at https://www.example.com then configure SNS to
    publish the SES notification topic to this HTTP endpoint:
    https://example.com/rails/action_mailbox/ses/inbound_emails

Rails

  1. Configure ActionMailbox to accept emails from Amazon SES:

     # config/environments/production.rb
     config.action_mailbox.ingress = :ses
  2. Configure which SNS topic will be accepted and what region.

    Note: The bucket's region, which stores the emails, does not need to match
    the SNS topic's region.

     # config/environments/production.rb
     config.action_mailbox.ses.subscribed_topic = 'arn:aws:sns:us-west-2:012345678910:example-topic-1'
     config.action_mailbox.ses.s3_client_options = { region: 'us-east-1' }

SNS Subscriptions will now be auto-confirmed and messages will be automatically
handled via ActionMailbox.

Note: Even if you manually confirm subscriptions, you will still need to provide
a list of subscribed topics; messages from unrecognized topics will be ignored.

See ActionMailbox documentation for full usage information.

Testing

Two RSpec request spec helpers are provided to facilitate testing
Amazon SNS/SES notifications in your application:

  • action_mailbox_ses_deliver_subscription_confirmation
  • action_mailbox_ses_deliver_email

Include the Aws::ActionMailbox::SES::RSpec extension in your tests:

# spec/rails_helper.rb

require 'aws/action_mailbox/ses/rspec'

RSpec.configure do |config|
  config.include Aws::ActionMailbox::SES::RSpec
end

Configure your test environment to accept the default topic used by the provided helpers:

# config/environments/test.rb
config.action_mailbox.ses.subscribed_topic = 'topic:arn:default'

Example Usage

# spec/requests/amazon_emails_spec.rb
RSpec.describe 'amazon emails', type: :request do
  it 'delivers a subscription notification' do
    action_mailbox_ses_deliver_subscription_confirmation
    expect(response).to have_http_status :ok
  end

  it 'delivers an email notification' do
    action_mailbox_ses_deliver_email(mail: Mail.new(to: '[email protected]'))
    expect(ActionMailbox::InboundEmail.last.mail.recipients).to eql ['[email protected]']
  end
end

You may also pass the following keyword arguments to both helpers:

  • topic: The SNS topic used for each notification (default: topic:arn:default).
  • authentic: The Aws::SNS::MessageVerifier class is stubbed by these helpers;
    set authentic to true or false to define how it will verify
    incoming notifications (default: true).