ActionMailer 使い方

  1. いきなりメール設定をしていく(今回はgmailを扱う前提)

    config/environments/development.rb

  config.action_mailer.raise_delivery_errors = true ← メールでエラーが出た時にログを出してくれる
  host = 'localhost:3000'
  config.action_mailer.default_url_options = { host: host}
  #mail設定

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    port: 587,
    domain: 'gmail.com',
    user_name: 'hogehoge@gmail.com',     ←gmailにログインする時のアドレス
    password: 'gmailのpassword',
    authentication: 'plain',
    enable_starttls_auto: true
  }

ActionMailerクラスを生成する

$ rails g mailer notice_mailer send_event_participation(←メソッド)     で以下のファイルができるのでそれぞれいじっていく

app/mailers/notice_mailer.rb app/mailers/application_mailer.rb app/views/notice_mailer/send_event_participation.html.slim    app/views/notice_mailer/send_event_participation.text.slim   spec/mailers/notice_mailer_spec.rb

spec/fixtures/message/send_event_participation

spec/mailers/previews/notice_mailer_preview.rb

3. メールの共通設定をするapplication_mailer.rbをいじる

class ApplicationMailer < ActionMailer::Base# 全メーラーの共通設定
  default from: "noreply@example.com",
          bcc:      "sample+sent@example.com",
          reply_to: "sample+reply@example.com"
  #####layout 'mailer'  ← template errorが出たりしたらコメントアウトしてください
end

4. notice_mailer.rbをいじる

class NoticeMailer < ApplicationMailer
  #イベントに参加した時に詳細をメールで送信する

  def send_event_participation(user, reservation)
    @user = user
    @event = Event.find(reservation.event_id).title
    mail to: user.email,
         subject: 'イベントに参加しました!'
  end
end

5. コントローラー側にメールを送信するタイミングを記述 reservation_controller.rbのcreateメソッドで使います(イベント予約の受付をメールで確認するイメージ)一例

def create
    reservation = Reservation.find_or_initialize_by(user_id: current_user.id, event_id: params[:id])
    if reservation.save
      NoticeMailer.send_event_participation(current_user, reservation).deliver_now #メール送信部
      redirect_to event_path(params[:id]), :flash => { :success => '参加予約完了しました!'}
    else
      flash.now[:error] = 'すでに参加済みです'
      redirect_to event_path(params[:id])
    end
  end

6.メソッド名に関連したsend_event_participation.html.slimにメールの内容を書く

p #{ @user.name }さんこんにちは!
p #{ @event }のイベントに参加受付を完了しました。

これで送信受信が正常にできるはず!!!!!!!

本番環境でのメール送信

上記の設定のままheroku環境でメールを送信したところ見事にエラー発生... 当たり前だが本番用の設定をする必要がある。 production環境からメール送信するために、「SendGrid」を使用する。

$ heroku addons:create sendgrid:starter ← アドオンをアプリに追加する

続いて、config/environments/production.rb内を設定していく

config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  host = '[herokuのサブドメインを入力].herokuapp.com'
  config.action_mailer.default_url_options = { host: host }
  ActionMailer::Base.smtp_settings = {
    :address => 'smtp.sendgrid.net',
    :port => '587',
    :authentication => :plain,
    :user_name => ENV['SENDGRID_USERNAME'],
    :password => ENV['SENDGRID_PASSWORD'],
    :domain => 'heroku.com',
    :enable_starttls_auto => true
  }

$ heroku config で環境変数を確認し SENDGRID_USERNAMEとSENDGRID_PASSWORDが設定されていることを確認。

$ git add -A  

$ git commit -m "メール本番環境設定"  

$ git push origin master

$ git push heroku master  

$ heroku run rake db:migrate

でおk!!!!

Action Mailer の基礎 | Rails ガイド