rails4.2.6配置傳送郵件

c3tc3tc3t發表於2016-07-04

除錯了很久,最後終於可以傳送了

1 在config/environments/development.rb檔案裡配置郵件資訊

config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :address              => "smtp.163.com",
      :port                 => 25,
      :domain               => '163.com',
      :user_name            => '你的郵箱登陸賬號',#例如 xxx@163.com
      :password             => '你的密碼',
      :authentication       => :login,
  }
View Code

 

2 建立一個基礎類 放在 app/mailers目錄下

1 class ApplicationMailer < ActionMailer::Base
2   default from: "郵箱地址" #這裡最好和上面配置檔案中你的郵箱賬戶一樣,否則可能有奇怪問題
3   layout 'mailer'
4 end

3 建立一個實際業務邏輯類

class OrderNotifier < ApplicationMailer

 
  def received
    
    mail to: "收件人地址", subject: 'Pragmatic Store Order Confirmation'
  end


end

4 view/order_notifier目錄下建立對應方法模板,例如 received.text.rb

1 <h1>OrderNotifier#received</h1>
2 
3 <p>
4   test received email
5 </p>

5 action中呼叫郵件類傳送郵件

OrderNotifier.received.deliver_now

 

具體可以看 agile web development with rails4 第五版,177頁詳細介紹,這裡只做摘記

相關文章