[Heroku] Rails 5.x 向け SendGrid メール送信の設定方法

Ruby on Rails 5.x で SendGrid (Heroku addon) 経由でのメール送信を設定したので、その方法をご紹介します。

SendGrid

本記事では Rails の ActionMailer でメール送信テストするまでを扱ってます。

SendGrid を使う前提

まず、メール配信サービスは SendGrid が特に評判がよかったので採用することにしました。

SendGrid の料金プラン

SendGrid の料金プランはこちらから確認できます。

月 12,000 通までは無料プランで利用できます。(2017/10/20 時点)

SendGrid API key は不要

Rails の ActionMailer を利用する場合、SendGrid API key は作成しなくて大丈夫です。

SendGrid Heroku アドオン追加

まず、SendGrid の Heroku アドオンを追加します。

heroku addons:create sendgrid:starter --app yourapp

Rails 向け SMTP 設定

Ruby on Rails にて SendGrid 経由でメール送信するための SMTP 設定は、以下のような感じです。

config/environments/production.rb

  # Setup the mailer config
  # Use SendGrid - Add-ons - Heroku
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.default_url_options = { host: 'yourapp.herokuapp.com' }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.smtp_settings = {
    user_name: ENV['SENDGRID_USERNAME'],
    password: ENV['SENDGRID_PASSWORD'],
    domain: 'herokuapp.com',
    address: 'smtp.sendgrid.net',
    port: 587,
    authentication: :plain,
    enable_starttls_auto: true
  }

config.action_mailer の設定項目はたくさんあるので、必要に応じて設定しましょう。

Rails の ActionMailer 経由でメール送信テスト

ActionMailer 作成

まず、TestMailer という名前の ActionMailer を rails generate コマンドで生成します。

$ rails generate mailer TestMailer
      create  app/mailers/test_mailer.rb
      invoke  haml
      create    app/views/test_mailer
   identical    app/views/layouts/mailer.text.haml
    conflict    app/views/layouts/mailer.html.haml
  Overwrite /Users/codenote/yourapp/app/views/layouts/mailer.html.haml? (enter "h" for help) [Ynaqdh] n
        skip    app/views/layouts/mailer.html.haml
      invoke  rspec
      create    spec/mailers/test_mailer_spec.rb
      create    spec/mailers/previews/test_mailer_preview.rb

テキストメールの本文用のテンプレートファイルを作成して、適当に本文を埋めます。

app/views/test_mailer/notify.text.haml

テストメール本文です。

rails console からメール送信テスト

Heroku でも rails console コマンドは使えます。

以下のように rails console で REPL から TestMailer.notify.deliver メソッドを実行して、メール送信テストしました。

$ heroku run rails console --app yourapp
Running rails console on ⬢ yourapp... up, run.8677 (Free)
Loading production environment (Rails 5.1.4)
 
irb(main):001:0> TestMailer.notify.deliver
I, [2017-10-20T13:41:57.721166 #4]  INFO -- :   Rendering test_mailer/notify.text.haml within layouts/mailer
I, [2017-10-20T13:41:57.725015 #4]  INFO -- :   Rendered test_mailer/notify.text.haml within layouts/mailer (3.6ms)
D, [2017-10-20T13:41:57.888214 #4] DEBUG -- : TestMailer#notify: processed outbound mail in 169.2ms
I, [2017-10-20T13:41:58.186642 #4]  INFO -- : Sent mail to [email protected] (298.1ms)
D, [2017-10-20T13:41:58.186805 #4] DEBUG -- : Date: Fri, 20 Oct 2017 13:41:57 +0000
From: from@example.com
To: your_email@example.com
Message-ID: <59e9fd25d9aa4_4a82f3c5336d@494abe47-1f52-48d9-abe0-679ebf9b174b.mail>
Subject: =?UTF-8?Q?=E3=83=86=E3=82=B9=E3=83=88=E3=83=A1=E3=83=BC=E3=83=AB=E4=BB=B6=E5=90=8D?=
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: base64
 
44OG44K544OI44Oh44O844Or5pys5paH44Gn44GZ44CCCgo=
 
=> #<Mail::Message:35240900, Multipart: false, Headers: <Date: Fri, 20 Oct 2017 13:41:57 +0000>, <From: [email protected]>, <To: [email protected]>, <Message-ID: <59e9fd25d9aa4_4a82f3c5336d@494abe47-1f52-48d9-abe0-679ebf9b174b.mail>>, <Subject: テストメール件名>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: base64>>

以下は無事にメール受信できたスクリーンショットです。

Send test email using SendGrid

(おまけ) SendGrid Dashboard を開く

heroku コマンドで直接 SendGrid Dashboard ページを開くことができます。

heroku addons:open sendgrid --app yourapp

以上、Rails + SendGrid on Heroku でメール配信したい、現場からお送りしました。

参考情報