ローカル環境の設定
とりあえず、ローカル環境の設定gem letter_opener_web
をインストールする。
1
2
3
|
group :development do
gem 'letter_opener_web'
end
|
routes.rbの設定
1
2
3
4
|
# routes.rb
if Rails.env.development?
mount LetterOpenerWeb::Engine, at: "/letter_opener"
end
|
config/environments/development.rb
の設定
1
2
|
config.action_mailer.default_url_options = { host: 'localhost:3000' }
config.action_mailer.delivery_method = :letter_opener_web
|
以上で終わり。あとは、localhost:3000/letter_opener
にアクセスする。
herokuを使った本番環境の設定
sendridを使います。プラグインの導入。
1
|
$ heroku addons:create sendgrid:starter
|
herokuにクレジットカードを登録してないとこの時点で、エラーが表示されるので、その場合は、herokuでクレジットカードを登録します。
Sendgridの設定
SENDGRID_USERNAME
とSENDGRID_PASSWORD
を調べる。
1
2
|
$ heroku config:get SENDGRID_USERNAME
$ heroku config:get SENDGRID_PASSWORD
|
config/environments/production.rb
を設定
1
2
3
4
5
6
7
8
9
10
11
12
|
config.action_mailer.default_url_options = { host: '自分のHerokuアプリのドメイン' }
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.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
}
|
環境変数の設定
1
2
|
$ heroku config:set SENDGRID_USERNAME=ユーザー名
$ heroku config:set SENDGRID_PASSWORD=パスワード
|
ちゃんとできた確認。
config/initializers/devise.rb
config.mailer_senderの値を’noreply@yourdomain’に変更する。
新規登録した際に認証メールが送信されるようにする
user.rbに:confirmableを追加。
1
2
3
4
|
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
end
|
最低限のメール認証に必要なカラムを追加
1
|
$ rails g migration add_confirmable_to_devise
|
できたファイルを全部消して下のをコピペ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class AddConfirmableToDevise < ActiveRecord::Migration
def up
add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at, :datetime
add_column :users, :unconfirmed_email, :string
add_index :users, :confirmation_token, unique: true
# User.reset_column_information # Need for some types of updates, but not for update_all.
execute("UPDATE users SET confirmed_at = NOW()")
end
def down
remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
remove_columns :users, :unconfirmed_email # Only if using reconfirmable
end
end
|
migration fileを編集後rake db:migrateを実行
See Also