対象者
- deviseでアカウント登録した後に確認用メールが送られてこなくて困っている方
説明しないこと
- deviseのセットアップ
全体像(メール認証)
①メール認証を使える状態にする.
②Railsアプリでメール送信を行う為の設定(SMTPサーバーの設定)をする
①メール認証を使える状態にする.
①Userテーブル作成
rails g devise user
上記のコマンドのuserの部分は好きな名前でいいですがdevise(アカウント登録機能など)と紐づいているのがuserなのでわかりやすいようにuserとしています.
下記のファイルが作成されます
②models/user.rbファイルを開く デフォルトでは以下になっています
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable,:confirmable end
↓,:confirmableを追加する
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable,:confirmable end
これは、ユーザー登録の後にメールが送られて、メール内のURLをクリックする事で本登録となる仕組みです。
③マイグレーションファイルの編集
rails g devise user
このコマンドで作成された devise_create_user.rbファイルの中身の
t.string :confirmation_token t.datetime :confirmed_at t.datetime :confirmation_sent_at t.string :unconfirmed_email
この4行をコメントインする
# frozen_string_literal: true class DeviseCreateUsers < ActiveRecord::Migration[6.1] def change create_table :users do |t| # User_name t.string :name, null: false , unique: true # admin t.boolean :admin, default: false ## Database authenticatable t.string :email, null: false, default: "" t.string :encrypted_password, null: false, default: "" ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable # t.integer :sign_in_count, default: 0, null: false # t.datetime :current_sign_in_at # t.datetime :last_sign_in_at # t.string :current_sign_in_ip # t.string :last_sign_in_ip ## Confirmable # t.string :confirmation_token # t.datetime :confirmed_at # t.datetime :confirmation_sent_at # t.string :unconfirmed_email # Only if using reconfirmable ## Lockable # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at t.timestamps null: false end add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true add_index :users, :name, unique: true # add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true end end
最後に
rails db:migrate
Userテーブルカラムの設定をする。 これで①メール認証を使える状態にする. が終わりました。
②Railsアプリでメール送信を行う為の設定(SMTPサーバーの設定)をする
①が終わっただけではまだメールの送信はできません。 Railsアプリでメール送信を行う為の設定が必要
メール送信〜受信の流れ
全体の流れは.
①メール送信者がSMTPサーバーに対して「このユーザーにメール送りたい」と依頼する.
②SMTPサーバーがDNSサーバーに対してメール受信者のIPアドレスを取得するように依頼する.
③DNSサーバーがメール受信者のIPアドレスを取得しSMTPサーバーに教える.
④SMTPサーバーが3POP3サーバーにメールを送る.
⑤POP3サーバーがメールを受信者にメールを送信する.
SMTPサーバー=メールを送信・配信する為のサーバー.
POP3サーバー =メール受信の為のサーバー.
SMTPサーバーとPOP3サーバーをの総称の名前がメールサーバーという.
この中で、準備する必要があるのはActionMailerとSMTPサーバーです。ですが、ActionMailerの設定は、Devise側で既に行われています。ですので今回は、SMTPサーバーの準備のみ行います。
SMTPサーバーの設定は以下の記事が分かりやすかったので共有します。