LoginSignup
10
11

More than 5 years have passed since last update.

Amazon SES + aws-sdk-v1でメールをActionMailer経由で送信する。

Last updated at Posted at 2015-01-14

SDK version2はまだRails Friendlyではないらしい。

aws-sdk-core(aws-sdk v2)でActionMailer経由でメール送信をしようと思っていたが、v2ではまだ実現できないらしい。(もちろんv2のメソッドを使って送信はできる)

問い合わせてみた

@ShotaKameyama The v1 SDK has rails integrations built in that provide a :amazon_ses delivery method configuration option. It uses your AWS credentials to send using the API, and not via smtp. The plan to is to port this into v2. Until this happens, you can safely use both the V1 and the V2 SDK together in the same application (they use different namespaces).

The rails integrations are on our short list to do soon after 2.0 stable.

ということで、対応方法を軽く教えてもらえたのでその通りにやってみる。

My suggestion to use both v1 and v2 gems is to use the aws-sdk-v1 gem and the preview release of aws-sdk.

v1とv2はそれぞれ依存関係がなく、namespaceが違うのでそれぞれを同居させることができるらしい。


前提

  • AWS上でSESの設定が一通り完了していること
  • AWSのproductionのリミッターを解除してもらっていること
  • IAMの設定をしてあること。

実装編

まずは、GemfileにSDKを追加する。

# Gemfile

# version2のsdk
gem 'aws-sdk', '2.0.6.pre'
# version1のsdk
gem 'aws-sdk-v1'

Initializersにbasicな情報を渡す。

# config/initializers/aws_ses.rb
AWS.config(
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
    :region => 'ap-northeast-1',
    :ses => { :region => 'us-west-2' }
    )

sesのリージョンを us-west-2 にしたのはSESをサポートしているリージョンの中で東京から一番近そうだったから。(ですよね?)

# config/environments/production.rb

  # Mailer with Amazon SES
  config.action_mailer.delivery_method = :amazon_ses
  config.action_mailer.default_url_options = { :host => 'hogehoge.com' }

これだけ。だいぶ簡単に実装できるようだ。
v2のRails Friendlyな対応も楽しみですね。

10
11
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11