ECのウェブ担当者のメモ

ECサイトを運営管理している、WEB担当プログラマのメモ

スポンサーリンク

Rails4からGamil経由でメールを送信する方法

f:id:jun9632:20151120004113j:plain

Rail4でActionMailerにGamilを設定して、Rails経由でGmailからメールを送信する方法を説明します。 この方法を使うと無料で、サービスからメールを送信することができます。

今回は、Gmailのアカウントの取得方法は割愛します。

ActionMailerの設定

開発環境は、config/environments/development.rb
本番環境は、config/environments/production.rb

に設定をそれぞれ追加します。

config/environments/development.rb

以下のように、追加します

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :address => 'smtp.gmail.com',
      :port => 587,
      :domain => 'smtp.gmail.com',
      :user_name => 'your-gmail@gmail.com',
      :password => 'your-gmail-password',
      :authentication => :plain,
      :enable_starttls_auto => true
  }

config/environments/production.rb

以下のように、追加します

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default_url_options = { :host => 'your-domain' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :address => 'smtp.gmail.com',
      :port => 587,
      :domain => 'smtp.gmail.com',
      :user_name => 'your-gmail@gmail.com',
      :password => 'your-gmail-password',
      :authentication => :plain,
      :enable_starttls_auto => true
  }

[your-domain]、[your-gmail@gmail.com]、[your-gmail-password]は適時環境に合わせて差し替えてください。 また、後で詳しく説明しますが、[your-gmail-password]は アプリ用のパスワードを取得して設定するとエラーなく一発で設定が完了します。

Mailer クラスの作成

続いて、Mailer クラスの作成です。

以下のコマンドをターミナルから実行して、Mailer クラスとMailのViewのファイルも一気に作成します。 以下の例では、test_mailerにsuccessメソッドが追加されます。

rails g mailer test_mailer success

そして、実行すると、以下のようなファイルが生成されます。

create  app/mailers/test_mailer.rb
      create  app/mailers/application_mailer.rb
      invoke  erb
      create    app/views/test_mailer
      create    app/views/layouts/mailer.text.erb
      create    app/views/layouts/mailer.html.erb
      create    app/views/test_mailer/success.text.erb
      create    app/views/test_mailer/success.html.erb
      invoke  test_unit
      create    test/mailers/test_mailer_test.rb
      create    test/mailers/previews/test_mailer_preview.rb

「app/mailers/test_mailer.rb」がコントローラー部分、 「app/views/test_mailer/success.text.erb」と「app/views/test_mailer/success.html.erb」がそれぞれ、 textとhtml メールのviewになります。

テストメールの送信

app/mailers/test_mailer.rbを開いて、テストのため、メールの送信元と送信先を自分のアドレスに向けます。

class TestMailer < ApplicationMailer

  default from: "your-gmail@gmail.com"

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.test_mailer.success.subject
  #
  def success
    @greeting = "Hi"

    mail to: "your-gmail@gmail.com"
  end
end

本文の内容を変更するようであれば、

「app/views/test_mailer/success.text.erb」と「app/views/test_mailer/success.html.erb」を変更すると 送信される内容も変更されます。

この状態で ターミナルから

rails c

$ TestMailer.success.deliver

すると、以下のようなエラーになりました。

Net::SMTPAuthenticationError: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbutn

Gmailの認証でひかかってしまいます。

エラー対策

対応としては、Gmailの2段認証を設定して、アプリ用のパスワードを生成してそのパスワードを config/environments/development.rbのActionMailerの設定の password: の 'your-gmail-password' 部分を生成されたパスワードに変更します。

再度、ターミナルから、

$ TestMailer.success.deliver

すると、以下のようにメールが送信されます。

 => #<Mail::Message:70125299997500, Multipart: true, Headers: <Date: Thu, 19 Nov 2015 14:04:35 +0900>, <From: your-gmail@gmail.com>, <To: your-gmail@gmail.com>, <Message-ID: <564d5863cd24f_80013fc75145e6e41682c@imai-no-iMac.local.mail>>, <Subject: Success>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_564d5863cb43c_80013fc75145e6e416768"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>
2.0.0-p481 :002 >

実際に、受信メールも確認できると思います。