2016年6月15日 星期三

Published 上午10:50 by with 0 comment

Ruby on Rails - Devise 設定忘記密碼Email

Rails 本身就透過 Action Mailer 支援 E-mail.

這篇主要是在講解 如何設定 Devise 寄送忘記密碼的信件

本篇來源:

1. Rails Guides

2. ihower Ruby on Rails 實戰聖經

3. How To: Mass password reset and email notification

前情提要:

已經安裝過 gem devise
尚未設定 actionmailer



Step 1.

在/config/environments/production.rb


config.action_mailer.raise_delivery_errors = false 改成 true

Step 2.

在Step 那行code的下方新增:


config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: "http://localhost:3000"} #你的網址,絕對網址 
config.action_mailer.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => "587",
    :domain => "gmail.com",
    :authentication => "plain",
    :user_name => "example@gmail.com", #你的信箱
    :password => "123456", #信箱密碼
    :enable_starttls_auto => true
 }

這邊的範例使用 gmail 來設定

Step 3.

正如同上面所看到的會需要輸入使用的信箱跟密碼, 所以需要把這些資訊藏起來

所以我們會新增 email.yml 放在 congig/ 底下


production:
  address: "smtp.mailgun.org"
  port: 587"
  domain: "gmail.com"
  authentication: "plain"
  user_name: "example@gmail.com", #你的信箱
  password: "123456", #信箱密碼 
  enable_starttls_auto: true 

完成後我們要到 .gitignore 新增


/config/email.yml
回到原本的地方寫成


config.action_mailer.smtp_settings = config_for(:email).symbolize_keys
最後我們在/config/environments/production.rb


  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default_url_options = { host: "http://localhost:3000"}
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = config_for(:email).symbolize_keys
設定完environments之後

要開始建立一個 mailer 寄信程式

Step 4.

輸入:

rails generate mailer UserMailer
這會在 app/mailers/ 新增 user_mailer.rb

架構類似 controllers內的檔案

class UserMailer < ActionMailer::Base
    default :from => "http://localhost:3000" #你的網址
end
之後我們新增

def password_reset(user, token) 
    @resource = user 
    @token = token 
    mail(:to => user.email, 
         :subject => 'Password Reset Notification') 
end
到 user_mailer.rb 內

Step 5.

還有到 lib/tasks/ 新增 devise.rake 內容:

namespace :device do
  desc "Mass password reset and send email instructions"
  task mass_password_reset: :environment do
    model = ENV['MODEL'] || 'User'
    begin
      model_mailer = "#{model}Mailer".constantize
      model = model.constantize

      model.where(id: 1).each do |record|
        raw, enc = Devise.token_generator.generate(model, :reset_password_token)

          record.reset_password_token = enc
          record.reset_password_sent_at = Time.now.utc
          record.save

        # Send change notification (Ensure you have created #{model}Mailer e.g. UserMailer)
        model_mailer.password_reset(record, raw).deliver_now
      end
    rescue Exception => e
      puts "Error: #{e.message}"
    end
  end
end
Step 6.

到config/initializers/devise.rb 修改

config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' 
=> '12345@gamil.com' #你的信箱
到config/locales/devise.en.yml 修改

reset_password_instructions: 
 subject: "Reset password instructions"=>"信的主旨"
Step 7.

目前完成 production的功能, 但是在開發時也想要做測試 (development )

我們會安裝 letter_opener

到gemfile 目錄底下新增


gem "letter_opener", :group => :development
在/config/environments/development.rb 修改成

config.action_mailer.delivery_method = :letter_opener
 config.action_mailer.default_url_options = { :host => 'localhost:3000' }
這樣在開發模式底下就會另開新頁 顯示信的內容.

Step 8.

完成!!!










      edit

0 意見:

張貼留言