8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails + Heroku + ActionMailer

Last updated at Posted at 2019-05-14

development環境でのActionMailerの設定

開発環境で届くメール見るためのgemいれる

Gemfile.
group :development do
  gem 'letter_opener'
  gem 'letter_opener_web'
end
route.rb

  if Rails.env.development?
    mount LetterOpenerWeb::Engine, at: '/letter_opener'
  end

Mailer作成

rails generate mailer contact
development.rb
  config.action_mailer.perform_caching = false
  config.action_mailer.default_url_options = { host: 'localhost:3000' }
  config.action_mailer.delivery_method = :letter_opener_web

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
   address:              'smtp.gmail.com',
   port:                  587,
   domain:               'gmail.com',
   user_name:             ENV['GMAIL_USER_NAME'],
   password:              ENV['GMAIL_USER_PASSWORD'],
   authentication:       'plain',
   enable_starttls_auto:  true
  }


  config.action_mailer.raise_delivery_errors = false

ENV['GMAIL_USER_NAME']ENV['GMAIL_USER_PASSWORD']は環境変数にセット
自分のGmailのメアドとパスワード

Gmailのセキュリティーをゆるくする

スクリーンショット 2019-05-14 0.38.51.png
contact_mailer.rb
class ContactMailer < ApplicationMailer
  def send_mail(contact)  #相手に送信用
    @contact = contact
    mail(
      from: 'system@example.com',
      to: @contact.email,
      subject: 'XXX お問い合わせありがとうございます。'
    )
  end

  def receive_mail(contact)  #自分に送信用
    @contact = contact
    mail(
      from: 'system@example.com',
      to: 'XXX@gmail.com',  #自分のメアドベタがき
      subject: 'お問い合わせ通知'
    )
  end
end


本文

contact_mailer/send_mail.text.erb

<%= @contact.name %> ( <%= @contact.name_kana %> )様 お問い合わせありがとうございます。

【メールアドレス】 <%= @contact.email %>

【お問い合わせ内容】

<%= @contact.message %>
contact_mailer/receive_mail.text.erb

<%= @contact.name %> ( <%= @contact.name_kana %> )様 から問い合わせがありました。

【メールアドレス】 <%= @contact.email %>

【お問い合わせ内容】

<%= @contact.message %>
contacts_controller.rb
  def create
    @contact = Contact.new
    @contact.attributes = session[:contact]
      if @contact.valid?
         @contact.save
         ContactMailer.send_mail(@contact).deliver_now   ##ここでメール送信処理を行う(相手)
         ContactMailer.receive_mail(@contact).deliver_now  ##ここでメール送信処理を行う(自分)
      render :action => "thanks"
    else
      flash.now[:warning] = "※プライバシーポリシーに同意してください。" if params[:contact][:terms].nil?
      render :action => "new"
    end
  end

production環境でのActionMailerの設定(Heroku)

HerokuのアドオンSendGridを追加

heroku addons:create sendgrid:starter
スクリーンショット 2019-05-14 0.38.51.png

ENV['SENDGRID_USERNAME'ENV['SENDGRID_PASSWORD']の環境変数が上のコマンド一つでセットされる

production.rb

  config.action_mailer.perform_caching = false

  config.action_mailer.default_url_options = { :host => 'アプリ名.herokuapp.com' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :domain => "heroku.com",
    :address => "smtp.sendgrid.net",
    :port => 587,
    :authentication => :plain,
    :enable_starttls_auto => true
  }

Deviseのメール送信の機能使ってもActionMailerを作らないところ以外の設定自体は同じです。

8
8
0

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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?